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?

James Graham
  • 39,063
  • 41
  • 167
  • 242
Roi Yozevitch
  • 147
  • 3
  • 9

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,860
  • 13
  • 29