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.
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(_ => 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));
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.