日期:2014-05-18  浏览次数:20714 次

关于带参数的线程启动。
public class TestThread

  public ucTestScriptTree _tree=new ucTestScriptTree ();
  public void ForTest(ucTestScriptTree tn)
  {
  tn.BeginImport(tn.m_node, tn.list);
  }

  private void OnShown(object sender, EventArgs e)
  {
  ////1
  //Thread m_Thread = new Thread(new ThreadStart(this.ThreadBeginImport));
  //m_Thread.Name = "UpImport_Thread";
  //m_Thread.Start();

  ////2
  //Thread m_Thread = new Thread(new ParameterizedThreadStart(this.ForTest));
  //m_Thread.start(_tree);

  ////3
  ThreadPool.QueueUserWorkItem(new WaitCallback(this.ForTest), _tree);

  }
}

分别企图用以上3中方法启动这个带参数的线程,但是都没能成功。直接编译错误

请教各位,这个线程应该怎么才可以启动啊~?~

先谢谢了~

------解决方案--------------------
C# code

//將ForTest修改如下:
        public void ForTest(object state)
        {
            ucTestScriptTree tn = (ucTestScriptTree)state;
            tn.BeginImport(tn.m_node, tn.list);
        } 
//使用下面的代碼即可啟動線程,亦即樓主所列之第二種方法
            Thread m_Thread = new Thread(new ParameterizedThreadStart(ForTest));
            m_Thread.Start(_tree);

------解决方案--------------------
public ucTestScriptTree _tree = new ucTestScriptTree();
public void ForTest( object obj )
{
ucTestScriptTree tn = ( ucTestScriptTree )obj;
tn.BeginImport( tn.m_node, tn.list );
}