Solved

Multithreading - User Interface

Posted on 1997-04-22
4
340 Views
Last Modified: 2013-12-04
I need to write a multithreaded application. The main application thread is going to handle all the user interface. The worker thread is going to perform all the background tasks. It is a going to be a long process and there is a need to display to the user a choice to let the process continue or abort in-between. Also, a user needs to be informed as to what stage is the worker thread in. For example, if the worker thread is copying files, a message needs to be displayed that says "Now copying files...". When the status changes to something else, a new message needs to be displayed. I can handle the worker thread. The question is : In the user interface thread, what kind of dialog box/notification should I use so as to handle this situation ? The user interface is NOT written using the MFC classes. I need to use only win32 functions to handle all that. Please help.
0
Comment
Question by:psapra
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 200 total points
ID: 1396894
There is simple enougth method to display current state of background thread. Just use shared memory for bkgnd and user interface threads. For example:

// Shared variable, do not use  __thread keyword
char msg[120]="";

//Bkgnd thread
void BkgndThread(void)
{
   // Starting;
   strcpy(msg,"Thread started");
   //...
   // CopyFile
   strcpy(msg,"Now copying files...");
   //... etc.
}
void UserInterfaceThread()
{
   CreateTimer(1,5000)   // For example, update status in 5 sec
   CreateBackgroundThread();
}
LRESULT WindowProc(...)
{
   case WM_TIMER:
        SetText(statuscontrol,msg);
}

Of course, it may cause problems, ie SetText may be invoking while strcpy() performing string copying. To avoid this you can use EnterCriticalSection/LeaveCriticalSection before/after 'dangerous' code.

Also you can check 'exit flag' in bkgnd thread from time to time.

Or, use CreateEvent() & WaitForSingleObject() to perform all this task. I.e., bkgnd thread copies status string to msg and sets event to signaled state. User-interface thread waits for this event and displays status string when event occures.
0
 

Expert Comment

by:markusk
ID: 1396895
I think you should rather send messages from the thread to your main window than updating the control bar on a timer base. This will be more effective and flexible.
0
 

Author Comment

by:psapra
ID: 1396896
'Markusk' - I'd like to get some more details on sending messages between the worker thread and main thread.

NickRepin - I was thinking along the same lines as you had suggested. The only difference was that instead of a string message, I was deciding on a set of enumerated messages. I could declare a global volatile variable. The worker thread updates the value of this global variable. In the main thread, I can switch on the global variable's value and display the appropriate message. My question still remains - What call do I use to display a dialog box that does not wait for a user input like a modal dialog box ? Also this should be capable of handling a user input if 'Cancel' is clicked. How do I handle the creation & destruction of this dialog box ?

0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1396897
You should use WinAPI's CreateDialog() to display a modeless dialog instead of DialogBox(). To destroy a modeless dialog box use  DestroyWindow() function.

If your background thread uses linear code model (i.e. not messages handling) then you should call CreateDialog() in main or auxiliary (third) thread.
1. Create background thread
2..Create modeless dialog and store dialog's handle returned by CreateDialog() to thread-shared variable.
Now the main thread may continue processing of other job.

if user presses "Cancel" then dialog box procedure must use TerminateThread()/'abort-flag'/other to terminate background thread and DestroyWindow() to close itself .

While working, background thread may post via PostMessage() (SendMessage()) the enumerated messages directly to dialog box procedure to display its current status.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

22 Experts available now in Live!

Get 1:1 Help Now