我需要做多线程,而且线程需要传递参数,然后要停止指定的线程,我写的如下,如果不对,请大家帮忙,如果方法问题,请大家教教我public class SearchCollection : DictionaryBase
{
public clsThreadFun this [int i]
{
get {return (clsThreadFun)Dictionary[i];}
}
public ICollection Values
{
get {return Dictionary.Values;} }
public clsThreadFun Add(int i)
{
clsThreadFun o=new clsThreadFun(i);
Dictionary.Add(i,o);
return o;
}
}
//////////////////////////////////////////////////
public class clsThreadFun:IDisposable {
private int _ThreadID;
Thread _Thread=new Thread(new ThreadStart(SearchMain)); private volatile bool blnIsStopped;//停止线程参数
private volatile bool blnSuspended;//暂停线程参数 public bool IsStopped
{
get{ return blnIsStopped; }
} private int ID; public int IDt { get{ return ID;} set{ ID = value; } }
public void Dispose()
{
blnIsStopped=true;
} public clsThreadFun(int IDs) {
blnIsStopped = false;
blnSuspended = false; _ThreadID=IDs;
_Thread.Start();
ID = IDs; }
SearchMain()
{
while(!blnIsStopped)
{
//要运行的线程的函数
}
}
}我在主class里面写启动线程是
SearchCollection _SearchCollection=new SearchCollection();
clsThreadFun _Search=SearchCollection.Add(int.Parse(this.AllTaskShow.FocusedItem.Tag.ToString()));
{
public clsThreadFun this [int i]
{
get {return (clsThreadFun)Dictionary[i];}
}
public ICollection Values
{
get {return Dictionary.Values;} }
public clsThreadFun Add(int i)
{
clsThreadFun o=new clsThreadFun(i);
Dictionary.Add(i,o);
return o;
}
}
//////////////////////////////////////////////////
public class clsThreadFun:IDisposable {
private int _ThreadID;
Thread _Thread=new Thread(new ThreadStart(SearchMain)); private volatile bool blnIsStopped;//停止线程参数
private volatile bool blnSuspended;//暂停线程参数 public bool IsStopped
{
get{ return blnIsStopped; }
} private int ID; public int IDt { get{ return ID;} set{ ID = value; } }
public void Dispose()
{
blnIsStopped=true;
} public clsThreadFun(int IDs) {
blnIsStopped = false;
blnSuspended = false; _ThreadID=IDs;
_Thread.Start();
ID = IDs; }
SearchMain()
{
while(!blnIsStopped)
{
//要运行的线程的函数
}
}
}我在主class里面写启动线程是
SearchCollection _SearchCollection=new SearchCollection();
clsThreadFun _Search=SearchCollection.Add(int.Parse(this.AllTaskShow.FocusedItem.Tag.ToString()));
clsThreadFun _Search=SearchCollection.Add(5);错误就是这句
Thread _Thread=new Thread(new ThreadStart(SearchMain));改为
Thread _Thread;//构造函数中
_Thread = new Thread(new ThreadStart(SearchMain))