Interesting Multithreading Problem :: MFC

Status
Not open for further replies.
Hi.

I am stuck in the design process of a small program I am working on. Here is the scenario.

-----
- user select an option in the menu
- main thread creates a dialog box w/ process bar
- dialog box's OnInitDialog() sends a message to main frame
- main frame redirects to view
- view creates a worker thread
- worker thread goes through a for loop // (i = 0, 1 < 10000; ++i);
- worker thread posts a message to main frame on every iteration
- main frame redirects message to view
- view calls a function in dialog box to update process bar
-----

Okay. The design above works fine. Here is one drawback. Even though the worker thread does all the processing (for loop), the main thread is *inaccessible* as it calls the function in the dialog box to update the process bar.

I would like to redesign that part of the program so that even as the worker thread is processing the for loop and as *some thread* updates the process bar, the user can still navigate the program.

Do I need to create a worker thread to update the progress bar? Do I need a UI thread? The reason I kept the dialog box as part of main thread is because according to Prosise, it is best to keep UI related objects in main thread.

Thanks,
Kuphryn
 
Okay. I want to clarify the actual problem. I saw a response from a member that implied that my description was not easy to understand.

The design works fine.

- worker thread handles data processing
- main thread (view) sends messages to dialog box
- dialog box updates the progress bar

The problem is that I believe the process of updating the progress bar is *considered* as part of the main thread. I think the reason is because the dialog box itself is declared in view (main thread).

I need three *independent* processes.

1) data processing
2) dialog box/progress bar updating
3) main thread is idling thus keeps the program from "locking."

Problem: Program seems to "lock" even though the data processing is done in the worker thread.

Question: I believe the reason it seems to "lock" is because as main thread sends message to dialog box to update the progress bar and as the dialog box update the progress bar, the frame considers the entire process (message + updating) as being in the main thread. I need to know a way to make everything separate.

Kuphryn
 
Okay.

I have consider several different design schemes. One of which is passing a pointer of a dialog box created in view into the worker thread. There, the worker thread can update the dialog box's progress bar. However, I do not think that will solve the problem of "locking." The reason is the dialog box belongs to view, thus it is still apart of view and the main thread. That leaves me with two alternatives opposites of one another.

Alternative #1
- create the dialog box first
- message view after InitDialog()
- view active a function inside of the dialog box
- function instantiate a worker thread that is *part of dialog box*
- worker thread will update the progress bar directly

Alternative #2 (opposite #1)
- create a worker thread
- create a dialog box w/ progress bar *inside of worker thread*
- update the progress bar directly

From the two alternatives, which one do you think is best? I want to make the design as "safe" as possible i.e, non-volatile.

Please mention any ideas you may have.

Thanks,
Kuphryn
 

BitPoet

Ars Legatus Legionis
21,805
Moderator
Had to go and dig up some of my old notes...

1st - how often are you updating the progress bar? Are either of the threads calling Sleep()?

2nd - how are you passing messages between the two threads? There's a big difference between SendMessage() and PostMessage() (for window messages, SendThreadMessage() and PostThreadMessage() for thread messages).

PostMessage() just drops a message into the specified message queue, and keeps going.

SendMessage() puts the message in the queue, makes the context switch to the correct thread, processes the message, then switches back to the original thread
 
1) The progress bar is updated at random. There is not direct way to calculate a time unless the interval is from 0 - 10seconds.

I am not positive on whether any of the threads is calling Sleep(). Now that you brought that up, I did implement a Sleep(25) in one of the thread as a *test* in early implemention to test it out. I will definitely look over to make sure. I will post an update.

2)I usually use SendMessage() in the main thread. I only use PostMessage() in all other threads.

Kuphryn
 
Stoffel brought up a good point that it is not possible to operate a UI object outside of the UI class where you created it.

http://www.gamedev.net/community/forums/topic.asp?topic_id=91304

Another person asked if maybe I have included a Sleep(), which could cause the program to appear "locked." I honestly do not remember. I do remember using Sleep() to test the program during implementation as a test. I will look over the code and make sure there is not call to Sleep().

Kuphryn
 
Status
Not open for further replies.