Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I create a dialog project,there a time-consuming function in it.
when I runing the function ,the program will fake dead.
I need help!
Posted

Always use threading in such cases. Sorry, you got no more information just because your question is not detailed enough, but you better start re-writing your code as multithreaded first, all by yourself and ask a question when you face any problems — we will gladly try to help.

—SA
 
Share this answer
 
Comments
Albert Holguin 14-Dec-11 14:50pm    
Yep, thread the work out... +5
Sergey Alexandrovich Kryukov 14-Dec-11 14:52pm    
Thank you, Albert.
--SA
You may find this discussion useful, Re-SetTimer.aspx[^], it concerns long running code and keeping the GUI responsive. Read the entire thread as it discusses several ups and downs to the approach.

If you are new to programming windows, it would be good to learn threads and how to use them to process things outside of the GUI events / messages.

But if you are just looking for a quick way to fix whatever this program is and move on (like if it's homework), a message pump will do the trick.

--------

Updated to include the link I forgot the first time

--------

Updated again.

OK, my gut told me to not bother with this question at all. The comments on my solution / suggestion just confirm what my gut thought in the first place.

Threads vs Message Pumps are a religious war. There is no winner. There are people who swear that Threads are the only way to go and that all other forms of keeping the GUI responsive are bad practice and punishable in the most harsh way. Thread devotees seem willing to force the most novice programmer into learning about threading, synchronization, cross thread communication, etc right out of the box. Threads are a complex concept for most beginners and they should have their sea legs firmly in place before learning threads. Threads require a discipline to use effectively and not get screwed up and that discipline is picked up over time, not in Comp Sci 1.

Now, before I get crucified for not liking threads, let me say that I was around when threading got invented, I was the architect for a set of products for an embedded chip based kernel that included threading. I was my company's representative on the Posix Threading Committee and worked on the reference implementation for Posix Threads. I know threading well.

Threading is very useful and worth learning but it is not the solution to every problem. Message Pumps can be useful for folks who do the obvious, start processing when the "do it" button is clicked in their GUI and all they want to do is keep the screen / log window up to date as process proceeds. It even works fine if all you want to do is process a "Cancel" button during processing. Yes, you can get yourself into trouble but I can demonstrate those same problems will exist if you used the "do it" button to fire off a thread to do the work.

Back in the 80's when I was doing all that kernel work, we used to laugh at some folk's attempts to create as many threads as they could, the old saying was "if threads are good for you, 4,000 threads must be wonderful". So yes, you can misuse threads and one way I noted was to use threads to do the work and then also fire off multiple Message Pumps in threads too. Microsoft / MFC already gives you a Message Pump in the default GUI thread so unless it is *exactly that thread* that you have made compute intensive, there is no reason to add more to the application. And so, Albert, I completely disagree with you. The Message Pump, if you are going to use one, CANNOT be in it's own thread but be used inside the long running code in the GUI thread because you have pre-empted the one provided by MFC.

OK, I'm going to stop with this, as I said, there is no winner in a religious war. Upvote this, downvote this, whatever, I'm no going to bother revisiting this ever again.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 14-Dec-11 14:44pm    
Did you forget the link?
--SA
Chuck O'Toole 14-Dec-11 14:50pm    
Thanks, my bad :)
Sergey Alexandrovich Kryukov 14-Dec-11 14:57pm    
In your post, you note that this method is flawed by yourself. In my opinion, in choosing between threading and message pumping, only threading should be considered. This is unlikely that there is only one place where such pumping can be used, and using it systematically is even risky. I would not encourage this. Come to think about, threads are more straight-forward (sequential logic in each, if the concept is used correctly), easier to implement and debug, more reliable results and better performance (if not abused).

Thank you,
--SA
Chuck O'Toole 14-Dec-11 16:05pm    
Read my updated solution
Albert Holguin 14-Dec-11 14:52pm    
A message pump will do the trick? Not if it's on the same thread (although by definition, you may mean message pump on its own thread???)...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900