locked
where should I place my main code when using windows forms application? RRS feed

  • Question

  • Hi all,

    I guess I don't really understand how the code runs when we create a window (with a button) using the windows forms application. I want to run a loop every time a button is pressed and stop the loop when that button is pressed again. I can easily change the condition of the while loop with the button, but i don't know where to place my loop. If I place it in the event handler of the button, the program freezes (can't stop the loop anymore). I'm using c++ if that makes any difference.


    • Moved by spacewrangler Friday, September 19, 2008 7:22 PM more appropriate forum (Moved from Building Development and Diagnostic Tools for .Net to Visual C++ General)
    Thursday, September 18, 2008 5:35 PM

Answers

  • Windows programming is based on events.  After your program starts and creates its windows, you go idle, waiting for something interesting to happen.  The technical term is "entering the message loop".  Your user sees your windows and decides to do something with them, like click the mouse on a control.  Your program springs back alive, it executes an event handler, doing whatever the user expects to happen.  To go immediately idle again, waiting for the user's next move.

    The one thing you cannot do in this scenario is start a loop.  You will not get the next event until your program goes idle again.

    Hans Passant.
    • Marked as answer by Naisan_Yazdani Saturday, September 20, 2008 2:30 PM
    Saturday, September 20, 2008 1:17 AM

All replies

  • Seems you want to do some background work. Try the BackgroundWorker class. Don't block the main message queue.
    MSMVP VC++
    • Marked as answer by Naisan_Yazdani Saturday, September 20, 2008 2:30 PM
    • Unmarked as answer by Naisan_Yazdani Saturday, September 20, 2008 2:30 PM
    Friday, September 19, 2008 8:01 PM
  • Windows programming is based on events.  After your program starts and creates its windows, you go idle, waiting for something interesting to happen.  The technical term is "entering the message loop".  Your user sees your windows and decides to do something with them, like click the mouse on a control.  Your program springs back alive, it executes an event handler, doing whatever the user expects to happen.  To go immediately idle again, waiting for the user's next move.

    The one thing you cannot do in this scenario is start a loop.  You will not get the next event until your program goes idle again.

    Hans Passant.
    • Marked as answer by Naisan_Yazdani Saturday, September 20, 2008 2:30 PM
    Saturday, September 20, 2008 1:17 AM