I need to load each second (or two) new image.
Following code doesn't work:
System.Threading.Thread.Sleep(2000);
this.Image_LoadImage.Source = new BitmapImage(new Uri(@"D:\\connect2-1.gif"));
System.Threading.Thread.Sleep(2000);
this.Image_LoadImage.Source = new BitmapImage(new Uri(@"D:\\connect3-1.gif"));
What I see is that the app sleeps for 4 seconds and then the second image appears.
How can i do it? Thanks.
Use timer for this
private System.Threading.Timer timer;
public MainWindow()
{
InitializeComponent();
timer = new System.Threading.Timer(OnTimerEllapsed, new object(), 0, 2000);
}
private void OnTimerEllapsed(object state)
{
if (!this.Dispatcher.CheckAccess())
{
this.Dispatcher.Invoke(new Action(LoadImages));
}
}
private bool switcher;
private void LoadImages()
{
string stringUri = switcher ? @"D:\\connect2-1.gif" :
@"D:\\connect3-1.gif";
this.Image_LoadImage.Source = new BitmapImage(new Uri(stringUri));
switcher = !switcher;
}
Use a timer.
Calling thread sleep is gonna block the UI thread. Found this link for you:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With