Pass Parameters through ParameterizedThreadStart

前端 未结 7 1108
伪装坚强ぢ
伪装坚强ぢ 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:47
    // Parameters to pass to ParameterizedThreadStart delegate
    // - in this example, it's an Int32 and a String:
    class MyParams
    {
        public int A { get; set; }
        public string B { get; set; }
    
        // Constructor
        public MyParams(int someInt, string someString)
        {
            A = someInt;
            B = someString;
        }
    }
    
    class MainClass
    {
        MyParams ap = new MyParams(10, "Hello!");
        Thread t = new Thread(new ParameterizedThreadStart(DoMethod));
        t.Start(ap); // Pass parameters when starting the thread
    }
    
    0 讨论(0)
  • 2021-01-30 16:51

    Use overloaded Thread.Start method, which accepts object (you can pass your custom type or array if you need several parameters):

    Foo parameter = // get parameter value
    Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));
    thread.Start(parameter);
    

    And in DoMethod simply cast argument to your parameter type:

    private void DoMethod(object obj)
    {
        Foo parameter = (Foo)obj;
        // ...    
    }
    

    BTW in .NET 4.0 and above you can use tasks (also be careful with race conditions):

    Task.Factory.StartNew(() => DoMethod(a, b, c));
    
    0 讨论(0)
  • 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!");
      }
    }
    
    0 讨论(0)
  • 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;
     }
    
    0 讨论(0)
  • 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; };
      }
    
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题