Thread: signals?

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    signals?

    I have a code that has a lot of:
    Code:
    while(flag)
       Application.DoEvents()
    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.

    Thanx

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    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.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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?"

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    i don't understand. why can't you just use the KeyDown event?

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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.
    Woop?

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    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...

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    no need in remaking a loop that is already implemented via events.

  8. #8
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Right...Let windows tell you when the cancel button has been clicked, and react to it then.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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.

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Can you be a bit more specific though, without a case study it's hard to guage what you're referring to.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, I ll see first if I can solve this with event handlers and if this is too difficult I ll post again

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Consider using a BackgroundWorker so you're not blocking the main thread.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Magos View Post
    Consider using a BackgroundWorker so you're not blocking the main thread.
    Interesting, I ll check it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about signals handling
    By smoking81 in forum Linux Programming
    Replies: 10
    Last Post: 10-01-2008, 03:38 PM
  2. kill & stop signals
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 02:04 AM
  3. problem with signals
    By cnchybrid in forum C Programming
    Replies: 8
    Last Post: 04-05-2007, 04:01 PM
  4. keyboard capturing
    By xlnk in forum Linux Programming
    Replies: 4
    Last Post: 01-31-2003, 01:02 PM
  5. queued signals problem, C programming related
    By Unregistered in forum Linux Programming
    Replies: 3
    Last Post: 01-22-2002, 12:30 AM