首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于带参数的线程启动。解决办法

2012-01-13 
关于带参数的线程启动。public class TestThread{public ucTestScriptTree _treenew ucTestScriptTree ()

关于带参数的线程启动。
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 );
}

热点排行