Pass Parameters through ParameterizedThreadStart

前端 未结 7 1253
伪装坚强ぢ
伪装坚强ぢ 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:05

    Another way to archive what you want, is by returning a delegate within your function / method. Take the following example:

    class App
    {
      public static void Main()
      {
         Thread t = new Thread(DoWork(a, b));
         t.Start();
         if (t.IsAlive)
         {
             t.IsBackground = true;
         }
      }
    
      private static ThreadStart DoWork(int a, int b)
      {
          return () => { /*DoWork*/ var c = a + b; };
      }
    
    }
    

提交回复
热议问题