Solved

Thread and Dialog

Posted on 2001-06-26
5
472 Views
Last Modified: 2013-11-20
Hi, first of all, I know nothing about MFC programming, I was a unix/server programmer.
So I have this task:
1. Start the application and draw all the windows (already done)
2. Near last step of initialization, create a thread that broadcast a message to LAN and listen for responses for about 10 seconds. Can be no response, or can be many responses. Store any response into a data structure. Then returns.
3. Last step, start another thread to listen to a port until the application exits. When this application on another machine does #2 above, this thread will respond, then go back to listen again.

#2 and #3 are supposed to be some kinda class, or routine, or API to the main program, and are the ones that I have to write.
If #2 has response, the main program should display a dialog containing the response info (in data structure). When the dialog is displayed, user should not be able to click on any other window/dialog of the application, but, the application must be able to continue processing whatever that has started, e.g. search for a file. I heard about modal dialog, but not sure if it is applicable here.
My friend told me #2 should use SendMessage to tell the main program that it should display a dialog because I should never display a dialog from a thread, I asked what is the difference to use return code of #2 to tell the calling program (in this case, main) that it should display a dialog.
I'd appreciate if you can explain how my task can be done, and perhaps what MFC programmers will generally do to handle this task. Do #2 and #2 have any flaw, etc. Simple example, or algorithm will be even better!
Also, if you know any online MFC tutorial for dummy, please provide the url. The msdn site is just too overwhelming or I'm not looking at the right page...
0
Comment
Question by:kitwei
5 Comments
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
Somehow you have a lot of questions in one :(

>I should never display a dialog from a thread
Actually can't catch from where you can display a dialog if you do it not from the thread? Any way I would show dialogs not modal and I defenetrly would use SendMessage of PostMessage.

when I have to ask user for parameters just show dialog, than, when user pressed OK (for example) in the dialog I do PostMessage with user parameters to my working thread and close dialog.
0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
>>>>If #2 has response, the main program should display a dialog containing the response info (in data structure).
When the dialog is displayed, user should not be able to click on any other window/dialog of the application,

>>>>listen for
responses for about 10 seconds. Can be no response, or can be many responses. Store any response into
a data structure. Then returns. ??

does that mean that there is no need for that thread and it would exit ? or what ?

If #2 and #3 are sequential i.e. #3 start after #2, and supposing that the thread for #2 exits after completion , what you can do in the thread is just keep gathering the responses and put them into the data-structure.
When the thread is about to exit it can post a message to the MainWindow or (since it is to exit anyway, it can display the dialog itself and exit after the dialog ends, if however it doesnot exit after 10 seconds and has more work to do displaying a modal dialog is not preferable, since it would block the thread until the dialog ends).

The MainWindow upon receiving the message can start the thread for #3 and then display a Modal dialog having all the data, this way #3 continues in background (here i assume that #3 does all the work) and the Modal dialog would not let the user click any other window or whatever of the application.

If #2 does not finish after 10 seconds then it can continue after posting the message to MainApp and keep on going.

Note if you use SendMessage control won't return to the thread unless the sent message is handled and if inside the handler you are displaying a modal dialog this can be very long , so prefer to use PostMessage in that case.

If you use Modeless dialogs you have to stop the user from clicking anywhere else on the application yourself.
0
 
LVL 22

Accepted Solution

by:
ambience earned 300 total points
Comment Utility
>>, I asked what is the difference to use return
code of #2 to tell the calling program (in this case, main) that it should display a dialog.

If you are new to Windows probably the first thing you must understand is its event-based programming model, you react to events (like arrival of a message etc.).

When you think of passing the return type of a function to another function you assume that the function has completed by the time it returns something. This means that the function is blocking and would return control to the caller after completion.

By making #2 a thread you imply #2 to continue execution on its own , so if #2 is a function that creates a thread it can return even before the created thread has started excution , and it is only after the thread finishes its work that you will show up a dialog , so either #2 creates a thread and waits for it to finish otherwise #3 will get called before #2 has even completed and this may lead to chaos , as in your case a dialog box may appear pre-maturely saying that it has found no response etc.

And if #2 has to wait for the thread to finish then perhaps you can overrule the need for a thread for #2 altogether (since thread are there to do work in parallel .. virtualy parallel i mean).

hope you understand that ..:)
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
hi kitwei,
Are you still interested in this Question?  Is there something specific that you need explained?

-- Dan
0
 
LVL 23

Expert Comment

by:Roshan Davis
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : ambience

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

728 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

10 Experts available now in Live!

Get 1:1 Help Now