Solved

Download Progress Bar

Posted on 2003-10-22
17
566 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
  • +2
17 Comments
 
LVL 48

Expert Comment

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

Author Comment

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

Expert Comment

by:AlexFM
ID: 9600307
In the main UI thread. This is a classic way - main thread is responsible for UI, and worker threads are doing some job.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SGyves
ID: 9600357
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
ID: 9600798
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
ID: 9600867
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
ID: 9602568
Hummm....timer or messages. WHich way to go??
0
 

Author Comment

by:SGyves
ID: 9602659
If I were to go the message posting way.....would I use a pointer to the main thread to post the messages???
0
 

Author Comment

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

Expert Comment

by:Zaryn
ID: 9602840
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
ID: 9602956
>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
ID: 9603377
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
ID: 9604855
#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
ID: 9607491
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
ID: 9607526
If not...I will extract the HWND as described above
0
 

Author Comment

by:SGyves
ID: 9607649
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
ID: 9608955
Pass HWND and not dialog pointer.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is it possible to combine several text files into one big one? (redhat v5 enterprise) 4 114
deburging in oracle form 12 119
sameEnds challenge 3 195
withoutTen challenge 14 140
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.

734 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