C#带参数的线程

一、带一个参数

Thread thrd = new Thread (new ParameterizedThreadStart(StartWriteToText));
thrd.Start (strParameter);
static void StartWriteToText(object objParameter)
{
 // ....
}
private static void StartThreadWriteToText(object objParameter)
{
 ThreadParameter thrdParameter = (ThreadParameter)objParameter;
}
二、带多个参数(方法一)

struct ThreadParameter
{
 public string strFilePath;
 public string strSqlScript;
};

ThreadParameter thrdParameter = new ThreadParameter();
thrdParameter.strFilePath = "";
thrdParameter.strSqlScript = "";

Thread thrd = new Thread (new ParameterizedThreadStart(StartWriteToText));
thrd.Start (thrdParameter);

private static void StartThreadWriteToText(object objParameter)
{
 ThreadParameter thrdParameter = (ThreadParameter)objParameter;
 
 //......
}
三、带多个参数(方法二)
class ParameterClass
{
 public string strFilePath;
 public string strSqlScript;
 
 public void StartWriteToText()
 {
  //......
 }
}
ParameterClass pmterClass = new ParameterClass();
pmterClass.strFilePath = "";
pmterClass.strSqlScript = "";

Thread thrd = new Thread(new ThreadStart(pmterClass.StartWriteToText));thrd.Start();

posted on 2009-02-19 20:44  一路前行  阅读(924)  评论(0编辑  收藏  举报