2011-04-06 60 views
3

我试图编写处理试图访问从另一个线程控制时可能发生的所有情况/问题“SafeInvoke”方法的访问控制。我已经看到了很多解决方案和很多关于这个问题的问题,虽然对于大多数人来说有一些是足够好的,但他们都没有考虑到竞争条件(意味着它仍然有可能获得不需要的例外)。创建方式安全地从另一个线程

所以这是我到目前为止,我想是最好的,我可以,为什么我把一些IFS和尝试捕获评论。我也试图只捕获相关的异常,InvalidOperationException是一个可能出于各种各样的原因(包括Collection被修改)并且我不想压制这些异常(因为它们与安全调用无关)的异常。为了检查我是否基于异常的TargetSite.Name属性,我还查找了反射器中的实际抛出,以查看是否有其他可能导致异常的位置。

/// <summary> 
/// Safely invokes an action on the thread the control was created on (if accessed from a different thread) 
/// </summary> 
/// <typeparam name="T">The return type</typeparam> 
/// <param name="c">The control that needs to be invoked</param> 
/// <param name="a">The delegate to execute</param> 
/// <param name="spinwaitUntilHandleIsCreated">Waits (max 5sec) until the the control's handle is created</param> 
/// <returns>The result of the given delegate if succeeded, default(T) if failed</returns> 
public static T SafeInvoke<T>(this Control c, Func<T> a, bool spinwaitUntilHandleIsCreated = false) 
{ 
    if (c.Disposing || c.IsDisposed) // preliminary dispose check, not thread safe! 
     return default(T); 

    if (spinwaitUntilHandleIsCreated) // spin wait until c.IsHandleCreated is true 
    { 
     if (!c.SpinWaitUntilHandleIsCreated(5000)) // wait 5sec at most, to prevent deadlock 
      return default(T); 
    } 

    if (c.InvokeRequired) // on different thread, need to invoke (can return false if handle is not created) 
    { 
     try 
     { 
      return (T)c.Invoke(new Func<T>(() => 
      { 
       // check again if the control is not dispoded and handle is created 
       // this is executed on the thread the control was created on, so the control can't be disposed 
       // while executing a() 
       if (!c.Disposing && !c.IsDisposed && c.IsHandleCreated) 
        return a(); 
       else // the control has been disposed between the time the other thread has invoked this delegate 
        return default(T); 
      })); 
     } 
     catch (ObjectDisposedException ex) 
     { 
      // sadly the this entire method is not thread safe, so it's still possible to get objectdisposed exceptions because the thread 
      // passed the disposing check, but got disposed afterwards. 
      return default(T); 
     } 
     catch (InvalidOperationException ex) 
     { 
      if (ex.TargetSite.Name == "MarshaledInvoke") 
      { 
       // exception that the invoke failed because the handle was not created, surpress exception & return default 
       // this is the MarhsaledInvoke method body part that could cause this exception: 
       // if (!this.IsHandleCreated) 
       // { 
       //  throw new InvalidOperationException(SR.GetString("ErrorNoMarshalingThread")); 
       // } 
       // (disassembled with reflector) 
       return default(T); 
      } 
      else // something else caused the invalid operation (like collection modified, etc.) 
       throw; 
     } 
    } 
    else 
    { 
     // no need to invoke (meaning this is *probably* the same thread, but it's also possible that the handle was not created) 
     // InvokeRequired has the following code part: 
     //  Control wrapper = this.FindMarshalingControl(); 
     //  if (!wrapper.IsHandleCreated) 
     //  { 
     //   return false; 
     //  } 
     // where findMarshalingControl goes up the parent tree to look for a parent where the parent's handle is created 
     // if no parent found with IsHandleCreated, the control itself will return, meaning wrapper == this and thus returns false 
     if (c.IsHandleCreated) 
     { 
      try 
      { 
       // this will still yield an exception when the IsHandleCreated becomes false after the if check (race condition) 
       return a(); 
      } 
      catch (InvalidOperationException ex) 
      { 
       if (ex.TargetSite.Name == "get_Handle") 
       { 
        // it's possible to get a cross threadexception 
        // "Cross-thread operation not valid: Control '...' accessed from a thread other than the thread it was created on." 
        // because: 
        // - InvokeRequired returned false because IsHandleCreated was false 
        // - IsHandleCreated became true just after entering the else bloc 
        // - InvokeRequired is now true (because this can be a different thread than the control was made on) 
        // - Executing the action will now throw an InvalidOperation 
        // this is the code part of Handle that will throw the exception 
        // 
        //if ((checkForIllegalCrossThreadCalls && !inCrossThreadSafeCall) && this.InvokeRequired) 
        //{ 
        // throw new InvalidOperationException(SR.GetString("IllegalCrossThreadCall", new object[] { this.Name })); 
        //} 
        // 
        // (disassembled with reflector) 
        return default(T); 
       } 
       else // something else caused the invalid operation (like collection modified, etc.) 
        throw; 
      } 
     } 
     else // the control's handle is not created, return default 
      return default(T); 
    } 
} 

有一件事我不确定,如果IsHandleCreated = true,它会再次变成虚假吗?

我为IsHandleCreated添加了spinwait,因为我在控件的OnLoad事件中启动了任务<> s,并且可能在控件加载完成之前完成了任务。然而,如果加载控件需要花费超过5秒的时间,我仍然可以完成任务,而无需更新GUI(否则我会有很多线程等待可能不会再发生的事情)

如果您有任何优化建议或找到任何可能仍然存在问题的错误或场景,请让我知道:)。

回答

0

老实说,你当你在一个基本的应用程序访问控制/从UI线程你这样做可能checkings呢?可能不会,你只是编码,并期望控制存在,并没有被处置。你为什么这么多的检查呢?

这不是一个好主意,让多个线程访问用户界面,但如果你有没有其他办法,我会建议你使用Control.BeginInvoke。使用Control.BeginInvokeControl.IsInvokeRequired应该就够了。

其实我从来没有用过Control.IsInvokeRequired,我手里所访问将来自不同的线程并没有之前知道。

+0

我最近在升级工作框架以异步方式获取数据来填充一个网格。我有一个GridOverview抽象类与抽象方法来检索数据。数据的实际获取从概述加载时开始(我重写了OnLoad)。我不能总是调用,因为当句柄没有创建时失败。我不能总是假定代码不是同步使用的。我在一个由4人组成的团队中,所以我不能依靠它,它总是以相同的方式使用。 – drake7707 2011-04-06 17:54:22

+0

此外,当我只需要更新网格时我使用BeginInvoke,并且我不需要从控件获取值,但是数据获取有时基于下拉菜单或类似输入的SelectedIndex。此外BeginInvoke不会被解雇调用线程已完成,我需要依赖的事实是,任务完成后,网格将始终更新。 – drake7707 2011-04-06 18:00:12

+0

你说得对,虽然最好先取值,然后以上述值作为参数启动任务,而不是让任务取而代之。但有些情况并非如此简单,我不想每次都别无选择,只能在处理所有场景时使用调用,以免在某个随机点崩溃。 – drake7707 2011-04-06 18:13:58

相关问题