none
Timing Events in CLR Windows Form Application RRS feed

  • Question

  • Hi there,

    I am a 1st time CLR Windows Application Forms user (in C++ Visual Studio 2005) and I was creating a Simon Sez game for excercise. However, I cannot get the timer (I used the sleep function) to work. It gives some odd behaviour which I do not understand. Basically, all the events in a thread are executed once all sleep events are expired.

    For example, when I tested the following:

        private: System::Void pictureBox4_Click(System:Surprisebject^  sender, System::EventArgs^  e) {
                     pictureBox1->Image = Image::FromFile("..//BMP//1.bmp");
                     Sleep(1000);
                     pictureBox2->Image = Image::FromFile("..//BMP//1.bmp");
                 }

    I expected when one clicks pb4 that the color of pb1 immediately changed (i.e. 1.bmp is a green sheet) and that the color of pb2 changed after 1 second. However, what happened is that after 1 second both pb's changed. Can someone explain this behaviour and/or suggest me some proper code?

    Thnks.
    Thursday, April 24, 2008 10:54 AM

Answers

  • If you do:

    Code Snippet

    pictureBox1->Image = Image::FromFile("..//BMP//1.bmp");

    Application.DoEvents();
    Sleep(1000);
    pictureBox2->Image = Image::FromFile("..//BMP//1.bmp");

     

     

    you should see that things will not happen all at once.

    when you do sleep, you put the main thread to sleep and nothing will happen anymore, unless there are some other threads and I suspect you don't have any other, you are putting your application to sleep.

    The main thread is responsible to execute your code, but also, it is responsible to handle the painting, and handling the other window events. By doing the doevents, you are giving the main thread the chance to handle these GUI events.

     

    Now what I have done will make it work, but that does not mean it is a good solution.

    A better solution is to use a timer. Drag a timer control to your form and in the click handler start the timer.

    Attach a event handler to the timer and there you set the picturebox2 image.

    This way your application remains responsive at all times.

     

    Thursday, April 24, 2008 11:06 AM

All replies

  • If you do:

    Code Snippet

    pictureBox1->Image = Image::FromFile("..//BMP//1.bmp");

    Application.DoEvents();
    Sleep(1000);
    pictureBox2->Image = Image::FromFile("..//BMP//1.bmp");

     

     

    you should see that things will not happen all at once.

    when you do sleep, you put the main thread to sleep and nothing will happen anymore, unless there are some other threads and I suspect you don't have any other, you are putting your application to sleep.

    The main thread is responsible to execute your code, but also, it is responsible to handle the painting, and handling the other window events. By doing the doevents, you are giving the main thread the chance to handle these GUI events.

     

    Now what I have done will make it work, but that does not mean it is a good solution.

    A better solution is to use a timer. Drag a timer control to your form and in the click handler start the timer.

    Attach a event handler to the timer and there you set the picturebox2 image.

    This way your application remains responsive at all times.

     

    Thursday, April 24, 2008 11:06 AM
  •  Philip_Stuyck__ wrote:

    If you do:

    Code Snippet

    pictureBox1->Image = Image::FromFile("..//BMP//1.bmp");

    Application.DoEvents();
    Sleep(1000);
    pictureBox2->Image = Image::FromFile("..//BMP//1.bmp");

     

     

    you should see that things will not happen all at once.

    when you do sleep, you put the main thread to sleep and nothing will happen anymore, unless there are some other threads and I suspect you don't have any other, you are putting your application to sleep.

    The main thread is responsible to execute your code, but also, it is responsible to handle the painting, and handling the other window events. By doing the doevents, you are giving the main thread the chance to handle these GUI events.


    That gets me the following syntax error, but [code]Application:Big SmileoEvents();[/code] does get the job done instead. (I am working in C++, which I only edited into the original question a bit later...)

     


    Now what I have done will make it work, but that does not mean it is a good solution.



    I was aware that the sleep function hijacks the UI but in the case of a game of Simon Sez it is not such a big problem. I tried the timer, but I could not get it to work.


    A better solution is to use a timer. Drag a timer control to your form and in the click handler start the timer.

    I cannot get the timer to start. Obviously ...


    timer1->Start;



    ... doesn't work and I fail to create a pointer to the member. Could someone please point to me what I am doing wrong?



    Attach a event handler to the timer and there you set the picturebox2 image.



    I think I figure how this works.
    Thursday, April 24, 2008 11:32 AM
  •  

    Code Snippet

    Obviously ... timer1->Start; ... doesn't work

     

     

     

    timer1->Start();

     

    Start is a function, not a property.

     

    Thursday, April 24, 2008 1:49 PM