I have a code that has a lot of:
which is very inefficient. It makes my CPU go almost on 100% until the while loop ends.
Is there a way to make this a bit more efficient without really changing the code.
Is this possible? Should I use multiple threads?
I would like a general advice how to achieve the best performance when you have a situation when you have to wait for the user to press lets say a Cancel or Done button in order to continue.
Why not just wait to do whatever you have to do until the done or cancel button is hit? In the mean time the window will continue processing windows messages and stay responsive. If its a dialog box just make it modal and wait for the appropriate button message that you assign your dialog box buttons.
That's the thing. How would I wait? I wait with a while loop until the cancel button changes the flag. Isn't there a better way code for "wait until this button is pressed?"
i don't understand. why can't you just use the KeyDown event?
If you are just talking about handling an ok or cancel click on your own dialog. I have whipped up a quick example for you.
Attached is the project for Visual Studio 2005.
Just change the .pdf to a .zip.
I guess I dont understand why you are "waiting" in a while loop for somebody to trip a flag that is switched by hitting the cancel button. Why not take advantage of the fact that windows will tell your app when somebody has hit the cancel button, and from that you can do what you need to do in an event handler when somebody clicks on the cancel button, IE goto the next page or whatever you need to do. In the mean time the app is already processing its events...
no need in remaking a loop that is already implemented via events.
Right...Let windows tell you when the cancel button has been clicked, and react to it then.
The problem is that I have a lot of things that Cancel "cancels". I could though then use a flag to switch inside the cancel button event. Hmmm.
prog-bman: Even though I wasn't talking about your code is really handy and I ll definately use it :)
Well, I guess you guys are right. It will be kind of hard to change all the code and work like that though. But I ll do it at one point.
Can you be a bit more specific though, without a case study it's hard to guage what you're referring to.
Well, I ll see first if I can solve this with event handlers and if this is too difficult I ll post again
Consider using a BackgroundWorker so you're not blocking the main thread.
Interesting, I ll check it out
Originally Posted by Magos