C# 给多线程传递参数

首先,定义一个带object参数的方法,代码如下:

 public void ThreadMethod(object ParObject)
        {
            List<string> stid = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            toExport_Mutex.whatOperation optin = new toExport_Mutex.whatOperation();
          
            try
            {
                object[] objArray = (object[])ParObject;
                stid = (List<string>)objArray[0];
                con = (SqlConnection)objArray[1];
                cmd = (SqlCommand)objArray[2];
                optin = (toExport_Mutex.whatOperation)objArray[3];
            }
            catch (Exception ex)
            { }
            UpdateSQL(stid, con, cmd, optin);
        }

然后,单开一个线程并调用这个方法,并传入参数

 void MainUpdate(List<string> listTids, SqlConnection conn, SqlCommand cmd, toExport_Mutex.whatOperation op)
        {
            ParameterizedThreadStart pts = new ParameterizedThreadStart(ThreadMethod);
            Thread t = new Thread(pts);
            object o = new object[] { listTids, conn, cmd, op };
            t.Start(o);
        }

 

posted @ 2013-10-22 13:33  JasonGu0  阅读(739)  评论(0编辑  收藏  举报