Solved

Multithreading - User Interface

Posted on 1997-04-22
4
339 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

20 Experts available now in Live!

Get 1:1 Help Now