我写了2个class,为了给进程的运行的函数传递变量,我启动进程的写法如下
clsThreadFun Search=new clsThreadFun(5);
Thread test=new Thread(new ThreadStart(Search.SearchMain));
test.Start();其中clsThreadFun为进程要运行函数的class
请问如何才能够在clsThreadFun里面控制这个进程关闭和暂停,因为我要做多进程,如何区分它们的名字??我是新手,非常急。。谢谢
clsThreadFun Search=new clsThreadFun(5);
Thread test=new Thread(new ThreadStart(Search.SearchMain));
test.Start();其中clsThreadFun为进程要运行函数的class
请问如何才能够在clsThreadFun里面控制这个进程关闭和暂停,因为我要做多进程,如何区分它们的名字??我是新手,非常急。。谢谢
public class clsSubThread
{
private bool blnIsStopped;
private bool blnSuspended;
public bool IsStopped
{
get{ return blnIsStopped; }
} public clsSubThread
{
blnIsStopped = false;
blnSuspended = false;
} public void ThreadFun()
{
while( !blnIsStopped )
{
if( blnSuspended )
Thread.Sleep( 100 );
else
{
//Other operation here
}
}
}
public void Suspend()
{
blnSuspended = true;
}
public void Resume()
{
blnSuspended = false;
} public void Stop()
{
blnIsStopped = true;
}
}// Calling
clsSubThread mySubThread = new clsSubThread();
Thread thdSub = new Thread( new ThreadStart( mySubThread.ThreadFun ) );// Suspend thread
mySubThread.Suspend();// Resume thread
mySubThread.Resume();// Stop thread
mySubThread.Stop();
thdSub.Join();
private bool blnIsStopped;
private bool blnSuspended;改为:
private volatile bool blnIsStopped;
private volatile bool blnSuspended;
如test.Name="1";这样就能区分是那个线程
clsSubThread thread1 = new clsSubThread();
Thread thdSub1 = new Thread( new ThreadStart( thread1.ThreadFun ) );
thdSub1.Start();
clsSubThread thread2 = new clsSubThread();
Thread thdSub2 = new Thread( new ThreadStart( thread2.ThreadFun ) );
thdSub2.Start();//Stop thread 1
thread1.Stop();//Stop thread 2
thread2.Stop();
private bool blnIsStopped;
private bool blnSuspended;改为:
private volatile bool blnIsStopped;
private volatile bool blnSuspended;这个有什么用吗?