[英]How to use handler method at another thread on .Net 4.0
Good day! 美好的一天!
I try to execute handler method at another thread, but dont know how. 我尝试在另一个线程执行处理程序方法,但不知道如何。
My code: 我的代码:
OnMyEvent+=MyTestFunc;
void MyTestFunc(Object sende,SomeClass c)
{
... do work;
}
Please,tell me how to execute MyTestFunc at another thread. 请告诉我如何在另一个线程上执行MyTestFunc。
The simplest version for a fire and forget method (you don't need any return value) is to use the Thread Pool : ThreadPool.QueueUsersWorkItem
: 触发即忘方法(您不需要任何返回值)的最简单版本是使用线程池 :
ThreadPool.QueueUsersWorkItem
:
ThreadPool.QueueUsersWorkItem(_ => MyTestFunc(arg1, arg2));
(Use _
because a state object will be passed, but you don't need it.) If you want to do this for an event handler: (使用
_
因为将传递状态对象,但您不需要它。)如果要为事件处理程序执行此操作:
OnMyEvent += (x, y) => {
ThreadPool.QueueUsersWorkItem(_ => MyTestFunc(x, y));
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.