Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MultiThreaded Progress bar

MultiThreaded Progress bar

Scheduled Pinned Locked Moved C / C++ / MFC
announcement
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SamirSood
    wrote on last edited by
    #1

    I have a program in which i have a background process running and i have to update the progress of the Progress bar according to the activities of the Background process . Can Anyone suggest a solution. Samir Sood

    J H 2 Replies Last reply
    0
    • S SamirSood

      I have a program in which i have a background process running and i have to update the progress of the Progress bar according to the activities of the Background process . Can Anyone suggest a solution. Samir Sood

      J Offline
      J Offline
      Jon Hulatt
      wrote on last edited by
      #2

      put your "background process" in a worker thread. Then your main (UI) thread is free to redraw the screen. Signature space for rent. Apply Within.

      S T 2 Replies Last reply
      0
      • J Jon Hulatt

        put your "background process" in a worker thread. Then your main (UI) thread is free to redraw the screen. Signature space for rent. Apply Within.

        S Offline
        S Offline
        SamirSood
        wrote on last edited by
        #3

        Could u please post a code snippet of that. Samir Sood

        1 Reply Last reply
        0
        • S SamirSood

          I have a program in which i have a background process running and i have to update the progress of the Progress bar according to the activities of the Background process . Can Anyone suggest a solution. Samir Sood

          H Offline
          H Offline
          Hesham Amin
          wrote on last edited by
          #4

          Hi.. I know two ways to do so: The first: to pass a handle of a progress bar to the worker thread and use it as follows 1. Create a helper class that will hold data passed to the worker thread: class CData { HWND m_hWnd; //Athor class members.. } 2. In the dialog class create a member variable of type CData: private: CData MyData; 3. Before the call to AfxBeginThread() fill members of CData : { //.. //.. some of your code MyData.m_hWnd=(GetDlgItem(IDC_PROGRESS))->m_hWnd; //where IDC_PROGRESS is the ID of the progress bar.. //now call AfxBeginThread AfxBeginThread(WorkerFn,&MyData, /*Ather args..*/); } 4. In the Worker thread function write the following code: UINT WorkerFn(LPVOID pData) { CData* data=(CData*)pData; CProgressCtrl* progBar=CWnd::FromHandle(data->m_hWnd); //Now use progBar as you wish! progBar->SetRange(0,100); //etc } The second: Pass the handle of the dialog enstead of the progress bar then use: ::PostMessage(data->m_hWnd,WM_USER+1,0,0) and handle the message in the dialog.. Send again if you find any problems..

          S 1 Reply Last reply
          0
          • J Jon Hulatt

            put your "background process" in a worker thread. Then your main (UI) thread is free to redraw the screen. Signature space for rent. Apply Within.

            T Offline
            T Offline
            Todd Smith
            wrote on last edited by
            #5

            Or put your progress dialog in its own thread. Either way will work. Todd Smith

            1 Reply Last reply
            0
            • H Hesham Amin

              Hi.. I know two ways to do so: The first: to pass a handle of a progress bar to the worker thread and use it as follows 1. Create a helper class that will hold data passed to the worker thread: class CData { HWND m_hWnd; //Athor class members.. } 2. In the dialog class create a member variable of type CData: private: CData MyData; 3. Before the call to AfxBeginThread() fill members of CData : { //.. //.. some of your code MyData.m_hWnd=(GetDlgItem(IDC_PROGRESS))->m_hWnd; //where IDC_PROGRESS is the ID of the progress bar.. //now call AfxBeginThread AfxBeginThread(WorkerFn,&MyData, /*Ather args..*/); } 4. In the Worker thread function write the following code: UINT WorkerFn(LPVOID pData) { CData* data=(CData*)pData; CProgressCtrl* progBar=CWnd::FromHandle(data->m_hWnd); //Now use progBar as you wish! progBar->SetRange(0,100); //etc } The second: Pass the handle of the dialog enstead of the progress bar then use: ::PostMessage(data->m_hWnd,WM_USER+1,0,0) and handle the message in the dialog.. Send again if you find any problems..

              S Offline
              S Offline
              Stephen C Steel
              wrote on last edited by
              #6

              Multithreaded apps are simplest if only the main thread deals with drawing the UI, as in your second solution. There is, however, no need to introduce a new message handler in your dialog. Just have the worker thread post a messages directly to the progress control:

              Before starting the worker thread, save the window handle of the progress control in a variable that will be accessible to the worker thread (e.g. m_hProgressCtrl). Then in your worker thread code, post messages to update the progress control directly ::PostMessage (m_hProgressCtrl, PBM_SETRANGE, 0, MAKELPARAM (min, max)); ::PostMessage (m_hProgressCtrl, PBM_SETPOS, position, 0);

              Using ::PostMessage() (instead of the ::SendMessage() inside the CProgressBar member functions) means that the messages to update the progress controls will be queued for the main GUI thread. Stephen C. Steel Kerr Vayne Systems Ltd.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups