BloggerAds廣告

2016年10月26日 星期三

[C#]How to pass multi parameters to thread


Thread如何傳遞多個參數?

之前都一直用全域變數來處理

後來認真找了資料, 感覺還不錯的處理方式

範例程式
    ...
    string p1 = "Yada yada.";
    long p2 = 4715821396025;
    int p3 = 4096;
    object args = new object[3] { p1, p2, p3 };
    Thread b1 = new Thread(new ParameterizedThreadStart(worker));
    b1.Start(args);
    ...
    private void worker(object args)
    {
      Array argArray = new object[3];
      argArray = (Array)args;
      string p1 = (string)argArray.GetValue(0);
      long p2 = (long)argArray.GetValue(1);
      int p3 = (int)argArray.GetValue(2);
      ...
    }

參考網址 :http://stackoverflow.com/questions/831009/thread-with-multiple-parameters