How to call a method that takes multiple parameters in a thread?

前端 未结 4 2004
無奈伤痛
無奈伤痛 2021-02-08 07:54

I am building a C# Desktop application. How do I call a method that takes multiple parameters in a thread. I have a method called Send(string arg1, string arg2, string arg3) , I

4条回答
  •  忘了有多久
    2021-02-08 08:33

    You can define an intermediate method and helper object to do this:

    public void MethodToCallInThread(string param1, string param2)
    {
        ...
    }
    public void HelperMethod(object helper){
        var h = (HelperObject) helper;
        MethodToCallInThread(h.param1, h.param2);
    }
    

    And then you start the thread with the HelperMethod, not with MethodToCallInThread:

    var h = new HelperObject { param1 = p1, param2 = p2 }
    ThreadPool.QueueUserWorkItem(HelperMethod, h);
    

提交回复
热议问题