How would I play a separate GIF when a picture is clicked on?

Question

So I just started learning C# and I'm currently working on a simple Form application. What I wanted to do was when the GIF is clicked, it would switch to another GIF, and then back to the original after 1 second. But what is happening is nothing. It just stays the original GIF. If I try using message box's instead that works fine, its just trying to change the source of the GIF is the problem.

private void pictureBox1_Click(object sender, EventArgs e)
{
    /* pictureBox2.Show();
       Thread.Sleep(1000);
       pictureBox2.Hide(); */
    pictureBox1.Image = WindowsLogin.Properties.Resources.PenguinEXE;
    Thread.Sleep(1000);
    pictureBox1.Image = WindowsLogin.Properties.Resources.PenguinIdle;
}

I've also tried using the Show and Hide method but that didn't work either.


Show source
| c#   | winforms   2016-12-24 22:12 1 Answers

Answers ( 1 )

  1. 2016-12-24 22:12

    You can use async/await.

    Mark your method as async and use await Task.Delay(1000); instead of Thread.Sleep(1000);

    async private void pictureBox1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = WindowsLogin.Properties.Resources.PenguinEXE;
        await Task.Delay(1000);
        pictureBox1.Image = WindowsLogin.Properties.Resources.PenguinIdle;
    }
    
◀ Go back