Thread::Sleep question

Hello,
I've got a question about the Thread::Sleep command. I have a code which goes like so:

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)
         {
             System::ComponentModel::ComponentResourceManager^  resources = gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid);
             this->pictureBox5->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"Spin2")));
             Thread::Sleep(90);
             this->pictureBox5->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"Spin3")));
             Thread::Sleep(90);
             this->pictureBox5->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"Spin4")));
             Thread::Sleep(90);
             this->pictureBox5->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"Spin5")));
             Thread::Sleep(90);
             this->pictureBox5->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"Spin6")));
             Thread::Sleep(90);
             this->pictureBox5->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"Spin7")));
             Thread::Sleep(90);
             this->pictureBox5->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"Spin1")));
         };

Basicly it's meant to change the picture of a pictureBox to the pictures I specified, with the interval I specified (90 milliseconds) when button2 is clicked. The problem is it does not work like so; instead of sleeping, then changing the picture, then sleeping again and changing again and so on, it first sleeps all the ammounts specified together (90 ms*6) and then changes the pictures without delay.
My question is how can I solve this so that I can change a picture, then wait a certain ammount of time, then change the picture again and so on.

With Thanks, Gal Beniamini.


Answer this question

Thread::Sleep question

  • Mrrenzo

    Hello,
    Thank you very much for your answer, although I am still looking for a solution. The main problem is that when using an animated control I experience some problems; the application I work with supports transparicy but in a very low level... It creats a white frame on the picture. I thought maybe because there are only 7 frames I could change them manually but I see I can't...
    Oh well, I'll just find a better application.

    With Thanks,
                        Gal Beniamini.

  • kalinkula

    You can basically create a seperate thread for such procedure that just sleeps for what ever desired time you want and when it is alive, it can just execute any custome processes and it goes back to sleep once more.

    Thanks,
      Ayman Shoukry
      VC++



  • Sam Jarawan MSFT

    This will not work in this way.
    Setting the background to the picture control just invalidates the control, so it just sets a flag "need to be repainted" because you put the thread to sleep, this message never reaches the control. So it looks like that all time passes and than the last picture occurs.

    Use an animated control.
    Or force an update to the control. But I do not like this way.

  • cgian31

    But you have to be very careful in the design if some controls that are owned by other threads are involved!

  • Jiongxiong Chen

    Hello, I've got a different problem now;

    My code is the following:


    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)

    {

    System::ComponentModel::ComponentResourceManager^ resources = gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid);

    this->Controls->Add(this->pictureBox1);

    this->Controls->Add(this->pictureBox2);

    this->Controls->Add(this->pictureBox3);

    this->Controls->Add(this->pictureBox4);

    this->Controls->Add(this->pictureBox5);

    this->Controls->Add(this->pictureBox6);

    this->Controls->Add(this->pictureBox7);

    this->Controls->Add(this->pictureBox8);

    this->Controls->Add(this->pictureBox9);

    this->Controls->Add(this->pictureBox10);

    this->pictureBox10->BringToFront();

    this->pictureBox9->BringToFront();

    this->pictureBox8->BringToFront();

    this->pictureBox7->BringToFront();

    this->Controls->Add(this->button2);

    this->Controls->Add(this->button3);

    pictureBox5->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"Spin1")));

    pictureBox6->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"Spin1 Opposite")));

    Controls->Remove(groupBox1);

    this->ClientSize = System::Drawing::Size(1024, 768);

    this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"Background Grass")));

    while (PHP > 0 && CHP > 0)

    {

    Thread^ sleepThread;

    sleepThread->Sleep(400);

    //Damage

    Random^ dmg = gcnew Random;

    IDMG = dmg->Next(10, 15);

    PHP = PHP - IDMG;

    int Y = pictureBox7->Top::get();

    this->pictureBox7->Top::set(Y+IDMG);

    this->pictureBox7->Size = System::Drawing::Size(18, CHP);

    //Damage

    if (PHP == 0 || PHP < 0 && Lose == false)

    {

    MessageBox::Show("You lose!!!", "Defeat", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);

    Wins -= 1;

    Lose = true;

    }

    }

    };


     



    Basicly it's supposed to add pictureBoxs 1-10 and bring 7-10 to front, also it's supposed to add buttons 2 & 3, then change the background to the picture specified, change the size of the client, remove groupBox1 and then start my 'while' operation which is supposed to execute every 400 ms. The problem is... It doesn't do that. Instead it changes the form's background, changes the size and adds the controls but they are only partially visible (I can see only a grey square where they are supposed to be) and they can't be clicked... Is there a way to just set a delay of a certain time without all of those problems

    With Thanks,
                     Gal Beniamini


  • PriyaM

    ...Is there no way anybody knows of to do such a thing

    With Thanks,
                       Gal Beniamini.

  • jebz171339

    Hello,
    I have yet another question; is there a way to create a timed procedure which works every period of time specified and does custom processes

    With Thanks,
                        Gal Beniamini.

  • ostsee

    The PictureBox control automatically handles displaying images with time-based frames such as an animated GIF. If you want to write you own custom control, have a look at the ImageAnimator class.

  • Thread::Sleep question