WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变

来源:互联网 发布:python怎么安装离线库 编辑:程序博客网 时间:2024/05/29 07:38

本文说明WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变


先来看看C#中Timer的简单说明,你想必猜到实现需要用到Timer的相关知识了吧。

C# Timer用法有哪些呢?我们在使用C# Timer时都会有自己的一些总结,那么这里向你介绍3种方法,希望对你了解和学习C# Timer使用的方法有所帮助。

在C#里关于定时器类有下面3个:

1.定义在System.Windows.Forms里

2.定义在System.Threading.Timer类里 "

3.定义在System.Timers.Timer类里

这3种C# Timer用法的解释:

System.Windows.Forms.Timer应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console Application(控制台应用程序)无法使用。 

System.Timers.Timer和System.Threading.Timer非常类似,它们是通过.NET Thread Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。

System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。 


下面来看一个WPF实现颜色渐变的例子:

WPF的MainWindow.xmal文件内容:

<Window x:Class="MyWPFApp.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="70" Width="250" Loaded="Window_Loaded">    <Grid>        <TextBlock Height="36" HorizontalAlignment="Left"  Name="gc" Text="不问你是谁只是沉醉!" VerticalAlignment="Top" Width="230" FontSize="24">                <TextBlock.Foreground>                    <LinearGradientBrush>                             <GradientStop Color="Green"></GradientStop>                            <GradientStop x:Name="gcc1"  Color="Green" Offset="0.3"></GradientStop>                            <GradientStop x:Name="gcc2" Color="Blue" Offset="0.3"></GradientStop>                            <GradientStop Color="Blue" Offset="1"></GradientStop>                     </LinearGradientBrush>                </TextBlock.Foreground>        </TextBlock>    </Grid></Window>

对应的MainWindow.xaml.cs的处理代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes; namespace MyWPFApp{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            System.Timers.Timer t = new System.Timers.Timer(200);//实例化Timer类,设置间隔时间为200毫秒;               t.Elapsed += new System.Timers.ElapsedEventHandler(theout);  //到达时间的时候执行事件;             t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);                t.Enabled = true;  //是否执行System.Timers.Timer.Elapsed事件;  ,调用start()方法也可以将其设置为true          }        public void theout(object source, System.Timers.ElapsedEventArgs e)        {                 }    }}
上面代码中的 public void theout(object source, System.Timers.ElapsedEventArgs e)
        { 
        
        }

方法中应该写的是对界面UI的元素中的字体进行控制的代码,先来看看下面的这种方法的结果

 public void theout(object source, System.Timers.ElapsedEventArgs e)        {            this.gcc1.Offset += 0.1;            this.gcc2.Offset += 0.1;        }

此种情况下会出现异常,异常提示为:调用线程无法访问此对象,因为另一个线程拥有该对象。


出现上面的异常是因为多个线程在同时访问一个对象造成的,在网上查看了一些资料,说的是在C#2005后不再支持多线程直接访问界面的控件(界面创建线程与访问线程不是同一个线程),但是可以可以使用delegate来解决。

相应的解决方法如下:

WPF:Dispatcher.Invoke 方法,只有在其上创建 Dispatcher 的线程才可以直接访问DispatcherObject。若要从不同于在其上创建 DispatcherObject 的线程的某个线程访问 DispatcherObject,请对与 DispatcherObject 关联的 Dispatcher 调用 Invoke 或 BeginInvoke。需要强制线程安全的 DispatcherObject 的子类可以通过对所有公共方法调用 VerifyAccess 来强制线程安全。这样可以保证调用线程是在其上创建 DispatcherObject 的线程。

  代码:

  this.lbl.Dispatcher.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));
     this.lbl.Dispatcher.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));

Winfrom:Control.Invoke 方法 (Delegate),在拥有此控件的基础窗口句柄的线程上执行指定的委托。

  代码:

          this.lbl.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));   


所以可以按照下面这样(修改theout方法的内容)来解决刚才的问题:

   public void theout(object source, System.Timers.ElapsedEventArgs e)        {            this.gcc1.Dispatcher.Invoke(               new Action(                    delegate                    {                        if (this.gcc1.Offset < 1)                        {                            this.gcc1.Offset += 0.1;                        }                        else                        {                            this.gcc1.Offset = 0;                        }                    }               )         );            this.gcc2.Dispatcher.Invoke(                   new Action(                        delegate                        {                            if (this.gcc2.Offset < 1)                            {                                this.gcc2.Offset += 0.1;                            }                            else                            {                                this.gcc2.Offset = 0;                            }                        }                   )             );         }
这样就可以解决"调用线程无法访问此对象,因为另一个线程拥有该对象"的问题

运行效果如下:





原创粉丝点击