简体   繁体   中英

Implementing progress control using threading

I am new to the concept of threading in C, so I find it difficult to implement that in my function. I have a simple application in which I want to display a progress bar at a particular place. In a particular function I will read files(in a for loop) for some manipulations(regarding my application). While it's reading the files I want to display a progress bar, stating that it's in process of reading files. I know it should be done using the concept of threading, but I am not quite sure how to do it.

Create a worker thread in the main program and set the callback routine that does the file processing.
That routine also will calculate the percentage that is completed. Whenever that percent changes, post the
value as a window message which the main thread will catch and update the progress bar control.
You can define application inner messages like #define MSG_PROGRESS_VALUE (WM_USER + 1) .

Edit: sample,

#define MSG_PROGRESS_VALUE (WM_USER + 1)
#define MSG_WORKER_DONE    (WM_USER + 2)
...
DWORD WINAPI  jobroutine(LPVOID lpParameter) {
   while (TRUE) {
      // process files ...
      // calculate new percent
      if (newpercent != oldpercent) {
         PostMessage(mainwnd, MSG_PROGRESS_VALUE, 0, newpercent);
         oldpercent = newpercent;
      }
      ...
   }
   PostMessage(mainwnd, MSG_WORKER_DONE, 0, 0);
   return 0;
}
...
MainWndProc(...)  {
   switch (uMsg) {
   ...
   case MSG_PROGRESS_VALUE:
   // update progress bar value (lParam)
   break;
 ...
}
...
WinMain(...) {
   HANDLE worker = CreateThread(NULL, 0, jobroutine, NULL, NULL, NULL);
   ...
   // Start classic windows message loop
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM