0

I have a WPF application in C#. When someone touches an image (image1), I want the image to change (image2), delay 2 seconds and finally change to image3.

My code looks like this:

private void  ImageName_TouchDown(object sender, TouchEventArgs e)
    {
        BitmapImage image = new BitmapImage(new Uri("c:/3.jpg", UriKind.Absolute));
        ImageName.Source =image;
        Thread.Sleep(2000);
        image = new BitmapImage(new Uri("c:/4.jpg", UriKind.Absolute));
        ImageName.Source = image;
    }

I get the delay to work but it seems like c# update only image3 (4.jpg). It is like it cannot update the image source within the event handler. What should I do?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Roi Yozevitch
  • 197
  • 3
  • 13

1 Answers1

0

You could make your event handler async, and use Task.Delay instead of Thread.Sleep:

private async void ImageName_TouchDown(object sender, TouchEventArgs e)
{
    BitmapImage image = new BitmapImage(new Uri("c:/3.jpg", UriKind.Absolute));
    ImageName.Source =image;
    await Task.Delay(2000);
    image = new BitmapImage(new Uri("c:/4.jpg", UriKind.Absolute));
    ImageName.Source = image;
}
ThePerplexedOne
  • 2,920
  • 15
  • 30