c# - 每秒加载新图像

标签 c# .net wpf multithreading image

我需要每秒(或两个)加载新图像。

以下代码不起作用:

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/

相关文章:

c# - 使用 SelectionMode Multiple/Extended 跨多个嵌套列表框获取所有选定项

c# - 为什么我在这里收到 "' System.Windows.Forms.Control' 不包含 'Checked' ...”的定义?

c# - Image Control 不显示使用 OpenCVsharp 捕获的网络摄像头帧

c# - 在后面创建数据模板代码

c# - 从 Angular 前端 POST JSON 到 ASP.NET 后端

c# - Windows窗体应用程序-文本突出显示

c# - 为什么我不能在 C# 中使用 var 声明常量?

.net - 删除boo中的一个变量

.net - 如何获取我的进程加载的文件列表?

wpf - HTML 5 + CSS 中的 XAML 是什么