Click to See Complete Forum and Search --> : Threads


Thomas Atwood
April 15th, 1999, 02:18 AM
In the app i am currently working on, I use worker threads to perform a few functions that require a large amount of time to complete. This has always worked fine in the past, but now I need to create mfc dialogs from within the thread. This works fine as long as I do not switch focus to another app while the dialogs are being created. If I do so, an Assertation violation occurs. I figure that I need to create a user interface thread (hence the name), but i can't find any example code or documentation except Microsoft's (and we all know how great microsoft is at providing documentation). anyway I would really appreciate it if someone could point me in the right direction.
thanks, Thomas

MoMoPi
April 20th, 1999, 03:26 PM
A "User-interface" thread this means you must provide a message loop. A user-interface thread is commonly used to handle user input and respond to user events independently of threads executing other portions of the application. The main application thread (provided in your CWinApp-derived class) is already created and started for you. You may check you VC++ samples,one of them will give you the idea how to do that.

Randy C
April 20th, 1999, 03:58 PM
Well I have a lot to learn here to, but I ran into similar problems and began reading about the issue. It seems that there are a lot of ways you can get in trouble with ASSERTS passing C++ objects between threads, and even though it might work fine in the release version, you're asking for trouble anytime anything can be accessed by more then one thread without synchronization. The system Caret might be the global object that got you in trouble this time, but I think it could have been many things. Here's an alternative that the system seems to handle pretty well. At least it did for me. From within your worker threads, send user defined messages to a window or non modal dialog box in your application thread with PostMessage(), and hand enter your own message macros and member functions to intercept the messages. With the two 32 bit parameters you're allowed to pass, you can easily send all the dedicated messages you need, along with indexs for strings, data values, etc. This way, you're sending messages instead of actually calling creating and managing the dialog from within the thread. In response, the application can create dialog boxes, update fields, and even gather data. Since your 2nd thread is a worker and not a UI thread, you can still send data back via some shared data in a global class, with some carefully placed Critical Sections or Mutex objects.

--Randy C
* * * Second star to the Right!

Kanwardeep
April 21st, 1999, 01:03 AM
Hi!!!
I was using threads in my last project. According to that experience, I had to come to the conlusion that I cannot create a non-modal dialog box inside a thread, so I had to create it somewhere else globally and when I wanted to use it I had to use ShowWindow() inside the thread. But for modal dialog boxes there was no such problem. Can anybody explain this?
Regards,
Kanwardeep