ThreadStart.BeginInvoke throws NotSupportedException on Compact framework

Deadly 提交于 2020-01-11 07:01:19

问题


I'm working with threads on a compact framework project and have code that looks something like below. When I try to step into StartThreads(), a NotSupportedException is thrown. This seems a bit wierd, why is the exception thrown on the line calling StartThreads() and not inside, and what is it that's not supported on CF? I think its ThreadStart.BeginInvoke but that's not where the exception is actually being thrown.

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);
}

回答1:


The BeginInvoke mechanism is not supported in CF, along with the ThreadPool.

The reason you don't see the Exception where you expect is due to the way this is implemented. I am not totally sure about the details but BeginInvoke is not a normal method (of the Delegate class) but something that is injected at runtime (just guessing that last part).

The error occurs when the JIT compiler gets to work on the StartThreads method.




回答2:


delegate.BeginInvoke is not supported on the CF.

However the ThreadPool is supported. You can use the thread pool to achieve essentially the same behavior.

void SomeMethod()
{
   this.StartThreads();
}

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

if you want it to call a callback when finished I suggest you read up on the Asynchronous Programming Model



来源:https://stackoverflow.com/questions/1062930/threadstart-begininvoke-throws-notsupportedexception-on-compact-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!