0

I have a code like this in windows form , c++

private: System::Void button3_MouseUp(Object^  sender, MouseEventArgs^  e) {
         _run=false;
         }
private: System::Void button3_MouseDown(Object^  sender, MouseEventArgs^  e) {
         _run = true;
         MyAction();
         }

private: System::Void MyAction()
         {
           while(_run)
           {
               i=i+1;
            System::Console::WriteLine( i );
           }
         }

The motivation was to keep the value of "i" (i is an integer ) going up till the time I press the the button and stoping when I release the button . But one i press the button , value of i increses and the UI hangs and never stops , Any one have any suggession to solve this issue

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
sarath
  • 89
  • 1
  • 2
  • 8

1 Answers1

0

You need to pump the message loop in your MyAction method. In C# and VB it would be something like Application.DoEvents()

Since this all runs on the UI thread, your loop blocks execution of anything else on the UI thread, including the message pump. By calling Application.DoEvents (or the C++ equivalent) you let the window message pump (wndproc) run, which processes window messages and dispatches events (calls button3_MouseUp() when appropriate).

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • I managed to use a timer in the windows form , and set it to work on MouseDown event and stopped it on MouseUp event – sarath Feb 08 '14 at 03:32