Pass Parameters through ParameterizedThreadStart

前端 未结 7 1128
伪装坚强ぢ
伪装坚强ぢ 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 17:08

    lazyberezovsky has the right answer. I want to note that technically you can pass an arbitrary number of arguments using lambda expression due to variable capture:

    var thread = new Thread(
           () => DoMethod(a, b, c));
    thread.Start();
    

    This is a handy way of calling methods that don't fit the ThreadStart or ParameterizedThreadStart delegate, but be careful that you can easily cause a data race if you change the arguments in the parent thread after passing them to the child thread's code.

提交回复
热议问题