How to call the method in thread with arguments and return some value

前端 未结 7 1864
南旧
南旧 2020-12-09 00:14

I like to call the method in thread with arguments and return some value here example

class Program
{
    static void Main()
    {
        Thread FirstThread         


        
7条回答
  •  醉话见心
    2020-12-09 00:44

    There is much simpler way to execute function in separate thread:

    // Create function delegate (it can be any delegate)
    var FunFunc = new Func(fun1);
    // Start executing function on thread pool with parameters
    IAsyncResult FunFuncResult = FunFunc.BeginInvoke(1, 5, null, null);
    // Do some stuff
    // Wait for asynchronous call completion and get result
    int Result = FunFunc.EndInvoke(FunFuncResult);
    

    This function will be executed on thread pool thread, and that logic is completely transparent to your application. An in general, I suggest to execute such small tasks on thread pool instead of dedicated thread.

提交回复
热议问题