【问题标题】:WPF Multithreading: Dispatcher exceptionWPF 多线程:调度程序异常
【发布时间】:2014-10-02 17:32:28
【问题描述】:

我是 WPF 的新手,我注意到您不能直接从另一个线程访问 WPF 控件。相反,您必须使用 Dispatcher。看了一些stackoverflow的文章和问题,还是不知道为什么下一个代码不起作用。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        System.Threading.Thread thread = new System.Threading.Thread(
        new System.Threading.ThreadStart(
          delegate()
          {
              TransformGroup transform = new TransformGroup();

              RotateTransform rot = new RotateTransform(45);
              TranslateTransform trans = new TranslateTransform(500, 500);


              transform.Children.Add(rot);
              transform.Children.Add(trans);

              bOne.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                          delegate()
                          {
                              bOne.RenderTransform = transform;
                          }
                      ));
                  }
              ));


        thread.Start();
    }
}

xaml 在哪里:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Custom="http://schemas.microsoft.com/surface/2008" x:Class="Prueba.MainWindow"
    Title="MainWindow" Height="350" Width="525">
<Grid>

    <Custom:SurfaceButton Name="bOne" Content="SurfaceButton" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>

尝试修改 bOne.RenderTransform 时仍然抛出异常。

【问题讨论】:

    标签: dispatcher


    【解决方案1】:

    发现问题,我在这里发布正确的版本。

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            System.Threading.Thread thread = new System.Threading.Thread(
            new System.Threading.ThreadStart(
              delegate()
              {
    
    
                  bOne.Dispatcher.Invoke(
                            System.Windows.Threading.DispatcherPriority.Normal,
                            new Action(
                              delegate()
                              {
                                  TransformGroup transform = new TransformGroup();
    
                                  RotateTransform rot = new RotateTransform(45);
                                  TranslateTransform trans = new TranslateTransform(100, 100);
    
    
                                  transform.Children.Add(rot);
                                  transform.Children.Add(trans);
    
                                  bOne.RenderTransform = transform;
                              }
                          ));
                      }
                  ));
    
    
            thread.Start();
        }
    }
    

    我没有意识到转换必须在调用中。

    【讨论】:

      猜你喜欢
      • 2019-01-02
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      相关资源
      最近更新 更多