Pass Parameters through ParameterizedThreadStart

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

    class Program 
    {
      public static void Main() 
      {
        MyClass myClass = new MyClass();
        ParameterizedThreadStart pts = myClass.DoMethod;
        Thread thread1 = new Thread(pts);
        thread1.Start(20); // Pass the parameter
        
        Console.Read();
      }
    }
    
    class MyClass 
    {
      private int Countdown { get; set; }
    
      public void DoMethod(object countdown) // Parameter must be an object and method must be void
      {
        Countdown = (int) countdown; 
        for (int i = Countdown; i > 0; i--) 
        {
          Console.WriteLine("{0}", i);
        }
        
        Console.WriteLine("Finished!");
      }
    }
    

提交回复
热议问题