Pass Parameters through ParameterizedThreadStart

前端 未结 7 1131
伪装坚强ぢ
伪装坚强ぢ 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
    }
    

提交回复
热议问题