我需要每秒(或两个)加载新图像。
以下代码不起作用:
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"));
我看到的是应用休眠 4 秒,然后出现第二张图片。
我该怎么做? 谢谢。
最佳答案
为此使用定时器
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;
}
关于c# - 每秒加载新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5869114/