C# Call a method in a new thread

后端 未结 6 1281
甜味超标
甜味超标 2020-12-13 04:08

I am looking for a way to call a method on a new thread (using C#).

For instance, I would like to call SecondFoo() on a new thread. However, I would the

6条回答
  •  不思量自难忘°
    2020-12-13 04:35

    Unless you have a special situation that requires a non thread-pool thread, just use a thread pool thread like this:

    Action secondFooAsync = new Action(SecondFoo);
    
    secondFooAsync.BeginInvoke(new AsyncCallback(result =>
          {
             (result.AsyncState as Action).EndInvoke(result); 
    
          }), secondFooAsync); 
    

    Gaurantees that EndInvoke is called to take care of the clean up for you.

提交回复
热议问题