Pass Parameters through ParameterizedThreadStart

前端 未结 7 1291
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 16:32

I\'m trying to pass parameters through the following:

Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));

Any idea how to do th

7条回答
  •  梦谈多话
    2021-01-30 16:56

    Instead of creating a class to pass in multiple parameters as @user1958681 has done, you could use anonymous types, then just use the dynamic typing to extract your parameters.

    class MainClass
    {
        int A = 1;
        string B = "Test";
    
        Thread ActionThread = new Thread(new ParameterizedThreadStart(DoWork));    
        ActionThread.Start(new { A, B});
    }
    

    Then in DoWork

    private static void DoWork(object parameters)
    {
       dynamic d = parameters;
    
        int a = d.A;
        string b = d.B;
     }
    

提交回复
热议问题