Download Progress Bar IRC program is going to have the ability to download files...which it already does quite well in a separate thread. I have a dialog resource mad up for monitoring the progress of the download. It will update a CProgressCtrl and a percentage label. What is the best way to achieve this? Should I create the dialog insode of this thread and post messages to it? Or does it need its own thread.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The best way is creating dialog in the main thread and posting user-defined messages to it.
SGyvesAuthor Commented:
Create a dialog in the thread which is doing the processing or the main UI thread.
In the main UI thread. This is a classic way - main thread is responsible for UI, and worker threads are doing some job.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

SGyvesAuthor Commented:
OK...from the worker can I get messages to the Dialog? I dont think I can pass a pointer to the dialog...that would be an ASSERT wouldn't it??

Just looking over the comments, I would suggest you might consider using a timer. ...I know it's kind of cheesy, but you could EXPLICITLY specify how often you want the dialog updated.  The procedure to do this is quite simple.

Create a timer with the progress dialog. (InitInstance)
Have an OnTimer function to handle the timer "clicks."
In this function, do whatever simple math you want to use to decide what percentage you're at.
Update the bar and the text. (UpdateData)
Once the progress is done, and the dialog is closing, kill the timer.

Let me know if you have any questions.

Just was thinking.. ....Should probably give you just a little more 411.

Key things:

Create a timer using CreateTimer.
End it with KillTimer.
...I usually override the dialog's OnCancel to do dialog cleanup. ...If you have a more "sterile" way, please let me know.
...So I'd override OnCancel to accomidate the KillTimer request.

SGyvesAuthor Commented:
Hummm....timer or messages. WHich way to go??
SGyvesAuthor Commented:
If I were to go the message posting way.....would I use a pointer to the main thread to post the messages???
SGyvesAuthor Commented:
In short....AlexFM...I would like it if you would expand on your answer.
I would figure timers would be preferable. ....If you want to update messages, you just need to have the pointer to the event.  ...That's all.  

I think the other authors are suggesting you access the percent done through the main thread. ... Something like...

while (looping through your program's primary thread, or maybe even worker thread... ...You know the conditions - I don't.)
   if (condition)
      StatusDlg dlg;
      m_pMainWnd = &dlg;
      //StatusDlg must contain a CProgressCtrl as a member variable.  ...Done first through resource editor, then in the ClassWizard.
      //...Plus it has a happy line similar to "m_CProgressCtrl.SetRange(min,max);", where "min" and "max" are explicitly indicated values.

   //Other stuff

   dlg->UpdateBar(n);   //And UpdateBar takes n, and uses it for SetPos(n);

...Let me know if you have any questions.  Either solution should be sufficient for your problem.
>OK...from the worker can I get messages to the Dialog? I dont think I can pass a pointer to the dialog...that would be an ASSERT wouldn't it??

 You can save the Handle to your main dialog window as a
global variable, then post messages to it from the thread.

For example, in the main dialog:

        // Handle of current dialog window

and in the thread:



The dialog box should be created by your main thread.  The thread that handles the downloading of files should get the HWND of the dialog box, and after receiving some data, should do a quick PostMessage to that window with a custom message that updates the progress control.  Sound easy?  It is.

#define WM_PROGRESS_MESSAGE    WM_APP + 1          // 1, 2... - message number

From the worker thread:

PostMessage(hDlg, WM_PROGRESS_MESSAGE, n, 0);      // hDlg is progress dialog handle, n is some data

In the dialog:

// h-file

    // this part is managed by MFC Class Wizard
    // here you can add functions manually:
    LRESULT OnProgressMessage(WPARAM wParam, LPARAM lParam);

// cpp-file

BEGIN_MESSAGE_MAP(CProgressDialog, CDialog)
    // this part is managed by MFC Class Wizard
    // here you can add map entries manually:

// message handler implementation
LRESULT CProgressDialog::OnProgressMessage(WPARAM wParam, LPARAM lParam)
    // Update here progress bar using wParam
    return 0;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SGyvesAuthor Commented:
I know how do define a user message....I am going to assume that it is ok to pass a pointer to the dialog into my thread.
SGyvesAuthor Commented:
If not...I will extract the HWND as described above
SGyvesAuthor Commented:
Anyone know how to get a download speed average (i.e. KB/sec) ther has to be a way to use the system time or something
Pass HWND and not dialog pointer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.