【问题标题】:ThreadStart.BeginInvoke throws NotSupportedException on Compact frameworkThreadStart.BeginInvoke 在 Compact 框架上抛出 NotSupportedException
【发布时间】:2009-07-02 15:32:51
【问题描述】:

我正在一个紧凑的框架项目中使用线程,并且代码如下所示。当我尝试进入 StartThreads() 时,会引发 NotSupportedException。这似乎有点奇怪,为什么在调用 StartThreads() 的行上会抛出异常而不是在内部,CF 不支持什么?我认为它的 ThreadStart.BeginInvoke 但这不是实际引发异常的地方。

void SomeMethod()
{
  this.StartThreads(); // <- NotSupportedException is thrown here, I can't step into this method with the debugger
}

void StartThreads()
{
  ThreadStart threadStart = BeginDoStuff;
  threadStart.BeginInvoke(EndDoStuff, null);
}

【问题讨论】:

    标签: c# .net multithreading compact-framework


    【解决方案1】:

    CF 不支持 BeginInvoke 机制以及 ThreadPool

    您没有在预期的地方看到异常的原因是因为它的实现方式。我不完全确定细节,但 BeginInvoke 不是(委托类的)普通方法,而是在运行时注入的东西(只是猜测最后一部分)。

    当 JIT 编译器开始在 StartThreads 方法上工作时发生错误。

    【讨论】:

    【解决方案2】:

    CF 不支持delegate.BeginInvoke。

    但是支持线程池。您可以使用线程池来实现基本相同的行为。

    void SomeMethod()
    {
       this.StartThreads();
    }
    
    void StartThreads()
    {
        System.Threading.ThreadPool.QueueUserWorkItem(DoStuff);
    }
    

    如果您希望它在完成后调用回调,我建议您阅读Asynchronous Programming Model

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多