子线程访问UI线程问题

时间:2021-02-11 18:18:07
我想实现的是,点击一下Button后,窗口中的Image控件上循环显示我的一组图片(7张)。但是点击Button后程序未响应。
请高手帮忙,不胜感激!

代码如下:
using System.Windows.Threading;
using System.Threading;

namespace wpfApplication1
{
    public partial class MainWindow : Window
    {
        public delegate void delegate1();
        public MainWindow()
        {
            InitializeComponent();
        }

        public void RunImage()
        {
                BitmapImage sample1 = new BitmapImage();
                sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
                        + "..\\..\\Images\\1.png", UriKind.Absolute));
                this.image1.Source = sample1;

                Thread.Sleep(200);
                sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
                        + "..\\..\\Images\\2.png", UriKind.Absolute));
                this.image1.Source = sample1;
                Thread.Sleep(200);
                sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
                        + "..\\..\\Images\\3.png", UriKind.Absolute));
                this.image1.Source = sample1;
                Thread.Sleep(100);
                sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
                        + "..\\..\\Images\\4.png", UriKind.Absolute));
                this.image1.Source = sample1;
                Thread.Sleep(200);
                sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
                        + "..\\..\\Images\\5.png", UriKind.Absolute));
                this.image1.Source = sample1;
                Thread.Sleep(300);
                sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
                        + "..\\..\\Images\\6.png", UriKind.Absolute));
                this.image1.Source = sample1;
                Thread.Sleep(100);
                sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
                        + "..\\..\\Images\\7.png", UriKind.Absolute));
                this.image1.Source = sample1;
                Thread.Sleep(1000);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Normal, new delegate1(RunImage));
        }

    }
}

7 个解决方案

#1


该回复于2011-04-19 11:15:51被版主删除

#2


该回复于2011-04-19 13:24:54被版主删除

#3


该回复于2011-04-19 13:31:40被版主删除

#4


有线程资源保护,需要使用System.Threading.SynchronizationContext 或者 Dispatcher.BeginInvoke 类来将信息透到主线程。

SynchronizationContext  sc =  SynchronizationContext.Current;
通过 sc.Post(xx); 方法可以把子线程 资源传到 UI线程.

#5


System.Threading.Thread thread = new System.Threading.Thread(
             new System.Threading.ThreadStart(
               delegate()
               {
                   this.Dispatcher.Invoke(
                     System.Windows.Threading.DispatcherPriority.SystemIdle,
                     TimeSpan.FromSeconds(1),
                     new Action(
                       delegate()
                       {
                           RunImage();
                       }
                   ));
               }
                ));
            thread.Start();

#6


BeginInvoke和 Invoke方法所调用的委托都是在 UI 线程中执行的。
支持楼上的

#7


该回复于2012-03-06 16:23:58被版主删除

#1


该回复于2011-04-19 11:15:51被版主删除

#2


该回复于2011-04-19 13:24:54被版主删除

#3


该回复于2011-04-19 13:31:40被版主删除

#4


有线程资源保护,需要使用System.Threading.SynchronizationContext 或者 Dispatcher.BeginInvoke 类来将信息透到主线程。

SynchronizationContext  sc =  SynchronizationContext.Current;
通过 sc.Post(xx); 方法可以把子线程 资源传到 UI线程.

#5


System.Threading.Thread thread = new System.Threading.Thread(
             new System.Threading.ThreadStart(
               delegate()
               {
                   this.Dispatcher.Invoke(
                     System.Windows.Threading.DispatcherPriority.SystemIdle,
                     TimeSpan.FromSeconds(1),
                     new Action(
                       delegate()
                       {
                           RunImage();
                       }
                   ));
               }
                ));
            thread.Start();

#6


BeginInvoke和 Invoke方法所调用的委托都是在 UI 线程中执行的。
支持楼上的

#7


该回复于2012-03-06 16:23:58被版主删除