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

本文说明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文件内容:

 

[plain] view plaincopy
  1. <Window x:Class="MyWPFApp.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="70" Width="250" Loaded="Window_Loaded">  
  5.     <Grid>  
  6.         <TextBlock Height="36" HorizontalAlignment="Left"  Name="gc" Text="不问你是谁只是沉醉!" VerticalAlignment="Top" Width="230" FontSize="24">  
  7.                 <TextBlock.Foreground>  
  8.                     <LinearGradientBrush>   
  9.                             <GradientStop Color="Green"></GradientStop>  
  10.                             <GradientStop x:Name="gcc1"  Color="Green" Offset="0.3"></GradientStop>  
  11.                             <GradientStop x:Name="gcc2" Color="Blue" Offset="0.3"></GradientStop>  
  12.                             <GradientStop Color="Blue" Offset="1"></GradientStop>   
  13.                     </LinearGradientBrush>  
  14.                 </TextBlock.Foreground>  
  15.         </TextBlock>  
  16.     </Grid>  
  17. </Window>  


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

 

 

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;   
  14.   
  15. namespace MyWPFApp  
  16. {  
  17.     /// <summary>  
  18.     /// MainWindow.xaml 的交互逻辑  
  19.     /// </summary>  
  20.     public partial class MainWindow : Window  
  21.     {  
  22.         public MainWindow()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.   
  27.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  28.         {  
  29.             System.Timers.Timer t = new System.Timers.Timer(200);//实例化Timer类,设置间隔时间为200毫秒;     
  30.             t.Elapsed += new System.Timers.ElapsedEventHandler(theout);  //到达时间的时候执行事件;   
  31.             t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);      
  32.             t.Enabled = true;  //是否执行System.Timers.Timer.Elapsed事件;  ,调用start()方法也可以将其设置为true    
  33.         }  
  34.   
  35.         public void theout(object source, System.Timers.ElapsedEventArgs e)  
  36.         {   
  37.           
  38.         }  
  39.     }  
  40. }  

上面代码中的 public void theout(object source, System.Timers.ElapsedEventArgs e)
        { 
        
        }

 

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

 

[csharp] view plaincopy
  1. public void theout(object source, System.Timers.ElapsedEventArgs e)  
  2.        {  
  3.            this.gcc1.Offset += 0.1;  
  4.            this.gcc2.Offset += 0.1;  
  5.        }  

 

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


出现上面的异常是因为多个线程在同时访问一个对象造成的,在网上查看了一些资料,说的是在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方法的内容)来解决刚才的问题:

 

 

[csharp] view plaincopy
  1. public void theout(object source, System.Timers.ElapsedEventArgs e)  
  2.      {  
  3.          this.gcc1.Dispatcher.Invoke(  
  4.             new Action(  
  5.                  delegate  
  6.                  {  
  7.                      if (this.gcc1.Offset < 1)  
  8.                      {  
  9.                          this.gcc1.Offset += 0.1;  
  10.                      }  
  11.                      else  
  12.                      {  
  13.                          this.gcc1.Offset = 0;  
  14.                      }  
  15.                  }  
  16.             )  
  17.       );  
  18.          this.gcc2.Dispatcher.Invoke(  
  19.                 new Action(  
  20.                      delegate  
  21.                      {  
  22.                          if (this.gcc2.Offset < 1)  
  23.                          {  
  24.                              this.gcc2.Offset += 0.1;  
  25.                          }  
  26.                          else  
  27.                          {  
  28.                              this.gcc2.Offset = 0;  
  29.                          }  
  30.                      }  
  31.                 )  
  32.           );   
  33.      }  

这样就可以解决"调用线程无法访问此对象,因为另一个线程拥有该对象"的问题

 

运行效果如下:

转自:http://blog.csdn.net/cselmu9/article/details/8274556

posted @ 2013-04-11 13:45  poisson_notes  阅读(492)  评论(0编辑  收藏  举报