Solved

Download Progress Bar

Posted on 2003-10-22
17
536 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:SGyves
  • 8
  • 4
  • 3
  • +2
17 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
The best way is creating dialog in the main thread and posting user-defined messages to it.
0
 

Author Comment

by:SGyves
Comment Utility
Create a dialog in the thread which is doing the processing or the main UI thread.
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
In the main UI thread. This is a classic way - main thread is responsible for UI, and worker threads are doing some job.
0
 

Author Comment

by:SGyves
Comment Utility
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??
0
 

Expert Comment

by:Zaryn
Comment Utility
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.

0
 

Expert Comment

by:Zaryn
Comment Utility
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.

0
 

Author Comment

by:SGyves
Comment Utility
Hummm....timer or messages. WHich way to go??
0
 

Author Comment

by:SGyves
Comment Utility
If I were to go the message posting way.....would I use a pointer to the main thread to post the messages???
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:SGyves
Comment Utility
In short....AlexFM...I would like it if you would expand on your answer.
0
 

Expert Comment

by:Zaryn
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:r-k
Comment Utility
>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.
0
 
LVL 19

Expert Comment

by:Dexstar
Comment Utility
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*
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 350 total points
Comment Utility
#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

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

// cpp-file

BEGIN_MESSAGE_MAP(CProgressDialog, CDialog)
    //{{AFX_MSG_MAP(CProgressDialog)
    // this part is managed by MFC Class Wizard
    //}}AFX_MSG_MAP
    // here you can add map entries manually:
    ON_MESSAGE(WM_PROGRESS_MESSAGE, OnProgressMessage)
END_MESSAGE_MAP()

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

Author Comment

by:SGyves
Comment Utility
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.
0
 

Author Comment

by:SGyves
Comment Utility
If not...I will extract the HWND as described above
0
 

Author Comment

by:SGyves
Comment Utility
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
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Pass HWND and not dialog pointer.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now