我写了2个class,为了给进程的运行的函数传递变量,我启动进程的写法如下
clsThreadFun Search=new clsThreadFun(5);
Thread test=new Thread(new ThreadStart(Search.SearchMain));
test.Start();其中clsThreadFun为进程要运行函数的class
请问如何才能够在clsThreadFun里面控制这个进程关闭和暂停,因为我要做多进程,如何区分它们的名字??我是新手,非常急。。谢谢

解决方案 »

  1.   

    Sample code as follows://Define sub-thread class
    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();
      

  2.   

    补充一下,建议
    private bool blnIsStopped;
    private bool blnSuspended;改为:
    private volatile bool blnIsStopped;
    private volatile bool blnSuspended;
      

  3.   

    不会,在线程里出重新实例化类,再调用类中函数.线程启动时,可以给线程命个名字,
    如test.Name="1";这样就能区分是那个线程
      

  4.   

    to 不如说我有5个线程,要停止第二个,如何确定是哪个线程?我给你的是一个线程类,不同的线程有不同的对象对应,例如:
    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();
      

  5.   

    Mark.那要开启线程组来执行。来如何确定是第几个了。这个问题也应该考虑。
      

  6.   

    补充一下,建议
    private bool blnIsStopped;
    private bool blnSuspended;改为:
    private volatile bool blnIsStopped;
    private volatile bool blnSuspended;这个有什么用吗?