惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Heimdal Security Blog
P
Privacy International News Feed
S
Schneier on Security
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
Spread Privacy
Spread Privacy
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Scott Helme
Scott Helme
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
aimingoo的专栏
aimingoo的专栏
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
Help Net Security
Help Net Security
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Security Archives - TechRepublic
Security Archives - TechRepublic
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
N
News and Events Feed by Topic
Hacker News: Ask HN
Hacker News: Ask HN
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
V
V2EX
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
N
News | PayPal Newsroom
T
Tor Project blog
Cisco Talos Blog
Cisco Talos Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
U
Unit 42
C
Cybersecurity and Infrastructure Security Agency CISA
P
Palo Alto Networks Blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
博客园 - Franky
I
InfoQ
D
DataBreaches.Net
爱范儿
爱范儿
Y
Y Combinator Blog
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 眼里进了砂

Unity Application Block 1.2 学习笔记 [转] Unity Application Block 與 ASP.NET MVC 學習資源整理 [转] .NET2.0中WinForm自定义的程序配置[转] 使用BackgroundWorker组件进行异步操作编程[转] .NET页面事件执行顺序[转] 不用设置iis .net 实现urlrewrite[转] 利用XPath读取Xml文件 javascript中replace与正则表达式 c# BackgroundWorker组件介绍(属性、方法、事件) .net 2.0 BackgroundWorker类详细用法 .Net的线程同步方法一:ManualResetEvent .NET应用程序中异步调用Web Service的几种方法 come from: veryhappy(wx.net) [转][javascript] Google谷歌首页点点效果 AJAX 中Sys.WebForms.PageRequestManager的事件激发顺序 AJAX 中Sys.Net.WebRequestManager的事件激发顺序 Summary review about Problem.Design.Solution 2 Summary review about Problem.Design.Solution [转发]深入理解 __doPostBack CSS Tips
线程之间的通讯---SynchronizationContext [转]
眼里进了砂 · 2009-12-06 · via 博客园 - 眼里进了砂

(转载自http://www.cnblogs.com/Kevin-moon/archive/2009/01/13/1374353.html)

(英文相关:http://www.codeproject.com/KB/threads/SynchronizationContext.aspx)

理解SynchronizationContext


  SynchronizationContext 类是一个基类,可提供不带同步的自由线程上下文。 此类实现的同步模型的目的是使公共语言运行库内部的异步/同步操作能够针对不同的异步模型采取正确的行为。此模型还简化了托管应用程序为在不同的同步环境下正常工作而必须遵循的一些要求。同步模型的提供程序可以扩展此类并为这些方法提供自己的实现。(来自MSDN)
  简而言之就是允许一个线程和另外一个线程进行通讯,SynchronizationContext在通讯中充当传输者的角色。另外这里有个地方需要清楚的,不是每个线程都附加SynchronizationContext这个对象,只有UI线程是一直拥有的。
  这里你可能有个问题:对于UI线程来说,是如何将SynchronizationContext这个对象附加到线程上的呢?!OK,我们先从下面的代码开始,

[STAThread]
static void
 Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(
false
);// let's check the context here
    var context = SynchronizationContext.Current;
    
if (context == null
)
        MessageBox.Show(
"No context for this thread"
);
    
else

        MessageBox.Show(
"We got a context");// create a form
    Form1 form = new Form1();// let's check it again after creating a form
    context = SynchronizationContext.Current;if (context == null)
        MessageBox.Show(
"No context for this thread"
);
    
else

        MessageBox.Show(
"We got a context");if (context == null)
        MessageBox.Show(
"No context for this thread"
);

    Application.Run(

new Form1());
}

运行结果:
1、No context for this thread
2、We got a context

     
     从运行结果来看,在Form1 form = new Form1()之前,SynchronizationContext对象是为空,而当实例化Form1窗体后,SynchronizationContext对象就被附加到这个线程上了。所以可以得出答案了:当Control对象被创建的同时,SynchronizationContext对象也会被创建并附加到线程上。
     好的,我们既然已经基本了解了SynchronizationContext,接下来的事情就是使用它了!

如何使用SynchronizationContext


  应用程序有两个线程:线程A和线程B,不过线程B比较特殊,它属于UI线程,当这两个线程同时运行的时候,线程A有个需求:"修改UI对象的属性",这时候如果你是线程A,你会如何去完成需求呢?!


第一种方式:
     

     在线程A上面直接去操作UI对象,这是线程B说:"线程A,你真xx,你不知道我的特殊嘛!",然后直接抛给线程A一个异常信息,线程A得到异常后,一脸的无辜和无奈.....!

第二种方式:
  

InvokeRequired?!是的,当然没问题。(解释下,InvokeRequired属性是每个Control对象都具有的属性,它会返回true和false,当是true的时候,表示它在另外一个线程上面,这是必须通过Invoke,BeginInvoke这些方法来调用更新UI对象的方法,当是false的时候,有两种情况,1:位于当前线程上面,可以通过直接去调用修改UI对象的方法,2:位于不同的线程上,不过控件或窗体的句柄不存在。对于句柄是否存在的判断,可以通过IsHandleCreated来获取,如果句柄不存在,是不能调用Invoke...这些方法的,这时候你必须等待句柄的创建
通过InvokeRequired的实现方式如下:

using System;
using
 System.Drawing;
using
 System.Windows.Forms;
using
 System.Threading;public class MyFormControl : Form
   {
      
public delegate void
 AddListItem(String myString);
      
public
 AddListItem myDelegate;
      
private
 Button myButton;
      
private
 Thread myThread;
      
private
 ListBox myListBox;
      
public
 MyFormControl()
      {
         myButton 
= new
 Button();
         myListBox 
= new
 ListBox();
         myButton.Location 
= new Point(72160
);
         myButton.Size 
= new Size(15232
);
         myButton.TabIndex 
= 1
;
         myButton.Text 
= "Add items in list box"
;
         myButton.Click 
+= new
 EventHandler(Button_Click);
         myListBox.Location 
= new Point(4832
);
         myListBox.Name 
= "myListBox"
;
         myListBox.Size 
= new Size(20095
);
         myListBox.TabIndex 
= 2
;
         ClientSize 
= new Size(292273
);
         Controls.AddRange(
new
 Control[] {myListBox,myButton});
         Text 
= " 'Control_Invoke' example "
;
         myDelegate 
= new
 AddListItem(AddListItemMethod);
      }
      
static void
 Main()
      {
         MyFormControl myForm 
= new
 MyFormControl();
         myForm.ShowDialog();
      }
      
public void
 AddListItemMethod(String myString)
      {
            myListBox.Items.Add(myString);
      }
      
private void Button_Click(object
 sender, EventArgs e)
      {
         myThread 
= new Thread(new
 ThreadStart(ThreadFunction));
         myThread.Start();
      }
      
private void
 ThreadFunction()
      {
         MyThreadClass myThreadClassObject  
= new MyThreadClass(this
);
         myThreadClassObject.Run();
      }
   }
   
public class
 MyThreadClass
   {
      MyFormControl myFormControl1;
      
public
 MyThreadClass(MyFormControl myForm)
      {
         myFormControl1 
=
 myForm;
      }
      String myString;
public void
 Run()
      {
         
for (int i = 1; i <= 5; i++
)
         {
            myString 
= "Step number " + i.ToString() + " executed"
;
            Thread.Sleep(
400
);
            
//
 Execute the specified delegate on the thread that owns
            
//
 'myFormControl1' control's underlying window handle with
            
// the specified list of arguments.

            myFormControl1.Invoke(myFormControl1.myDelegate,
                                   
new
 Object[] {myString});
         }
      }
   }


    不过这里存在一个有争论的地方:这种方式必须通过调用Control的Invoke方法来实现,这就是说调用的地方必须有一个Control的引用存在。
  看下MyThreadClass类,这个类中就存在MyFormControl的引用对象。其实如果这个类放在这里是没有任务不妥之处的,但是如果把MyThreadClass类放在业务层,这时候问题就出现了,从设计角度来说,业务层是不允许和UI有任何关系,所以MyFormControl的引用对象绝对不能存在于MyThreadClass类,但是不让它存在,更新UI控件的需求就满足不了,这种情况下,我们如何做到一种最佳方案呢!?

第三种方式:
  本文的主角:SynchronizationContext登场了。解释之前,先让下面的代码做下铺垫,

public partial class Form1 : Form
{
    
public
 Form1()
    {
        InitializeComponent();
    }
private void mToolStripButtonThreads_Click(object
 sender, EventArgs e)
    {
        
// let's see the thread id

        int id = Thread.CurrentThread.ManagedThreadId;
        Trace.WriteLine(
"mToolStripButtonThreads_Click thread: " +
 id);// grab the sync context associated to this
        
//
 thread (the UI thread), and save it in uiContext
        
//
 note that this context is set by the UI thread
        
//
 during Form creation (outside of your control)
        
// also note, that not every thread has a sync context attached to it.

        SynchronizationContext uiContext = SynchronizationContext.Current;// create a thread and associate it to the run method
        Thread thread = new Thread(Run);// start the thread, and pass it the UI context,
        
//
 so this thread will be able to update the UI
        
// from within the thread

        thread.Start(uiContext);
    }
private void Run(object
 state)
    {
        
// lets see the thread id

        int id = Thread.CurrentThread.ManagedThreadId;
        Trace.WriteLine(
"Run thread: " +
 id);// grab the context from the state
        SynchronizationContext uiContext = state as SynchronizationContext;for (int i = 0; i < 1000; i++)
        {
            
//
 normally you would do some code here
            
//
 to grab items from the database. or some long
            
// computation

            Thread.Sleep(10);// use the ui context to execute the UpdateUI method,
            
// this insure that the UpdateUI method will run on the UI thread.


            uiContext.Post(UpdateUI, 
"line " + i.ToString());
        }
    }
/// <summary>

    
/// This method is executed on the main UI thread.
    
/// </summary>

    private void UpdateUI(object state)
    {
        
int id =
 Thread.CurrentThread.ManagedThreadId;
        Trace.WriteLine(
"UpdateUI thread:" +
 id);
        
string text = state as string
;
        mListBox.Items.Add(text);
    }
}

运行结果:

mToolStripButtonThreads_Click thread: 10
Run thread: 
3
UpdateUI thread:
10
UpdateUI thread:
10
UpdateUI thread:
10
UpdateUI thread:
10
 (x1000 times)

    程序首先在Form1窗体的mToolStripButtonThreads_Click事件中,获取当前的SynchronizationContext对象,然后启动另外一个线程,并且将SynchronizationContext对象传递给启动的线程,启动的线程通过SynchronizationContext对象的Post方法来调用一个委托方法UpdateUI,因为UpdateUI是执行在主UI线程上的,所以可以通过它来修改UI上对象的信息。
    怎么样!不错吧,现在我们可以把Control引用给抛弃了,哈哈!
    如果你去查下MSDN,会发现SynchronizationContext还有一个Send方法,Send和Post有什么区别?

Send VS Post,以及异常处理


首先看下异常处理的情况

private void Run(object state)
{
    
// let's see the thread id

    int id = Thread.CurrentThread.ManagedThreadId;
    Trace.WriteLine(
"Run thread: " +
 id);// grab the context from the state
    SynchronizationContext uiContext = state as SynchronizationContext;for (int i = 0; i < 1000; i++)
    {
        Trace.WriteLine(
"Loop " +
 i.ToString());
        
//
 normally you would do some code here
        
//
 to grab items from the database. or some long
        
// computation

        Thread.Sleep(10);// use the ui context to execute the UpdateUI method, this insure that the
        
// UpdateUI method will run on the UI thread.


        
try
        {
            uiContext.Send(UpdateUI, 
"line " + i.ToString());
        }
        
catch
 (Exception e)
        {
            Trace.WriteLine(e.Message);
        }
    }
}
/// <summary>

/// This method is executed on the main UI thread.
/// </summary>

private void UpdateUI(object state)
{
    
throw new Exception("Boom"
);
}

   当你运行的时候, 你可能希望在UI线程上面去抛出,但是结果往往出忽你的意料,异常信息都在Run方法的线程上被捕获了。这时候你可能想问:WHY?!
   解释之前,我们先看下,Send VS Post的结果:
   Send 方法启动一个同步请求以发送消息
   Post 方法启动一个异步请求以发送消息。    
   哈哈,异常处理的答案迎韧而解了吧!