Link to home
Start Free TrialLog in
Avatar of SGyves
SGyves

asked on

Download Progress Bar

Ok....my 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.
Avatar of AlexFM
AlexFM

The best way is creating dialog in the main thread and posting user-defined messages to it.
Avatar of SGyves

ASKER

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.
Avatar of SGyves

ASKER

OK...from the worker thread....how 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??
Hello,

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.

Avatar of SGyves

ASKER

Hummm....timer or messages. WHich way to go??
Avatar of SGyves

ASKER

If I were to go the message posting way.....would I use a pointer to the main thread to post the messages???
Avatar of SGyves

ASKER

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;
      dlg.DoModal();
      //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 thread....how 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
      g_hWnd=m_hWnd;

and in the thread:

  ::PostMessage(g_hWnd,WM_SOME_MESSAGE,0,0);

etc.
SGyves,

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.

Dex*
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SGyves

ASKER

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.
Avatar of SGyves

ASKER

If not...I will extract the HWND as described above
Avatar of SGyves

ASKER

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.