比如,用户点击bootton1,启动线程,
private void bootton1_Click(object sender, EventArgs e)
        {
             Thread imputThread = new Thread(new ThreadStart(ImputData));
            imputThread.Start();
        }用户点击botton2时结束次线程。应该怎么做。谢谢。

解决方案 »

  1.   

    private void bootton2_Click(object sender, EventArgs e) 
            { 
              //这里我要怎么写?谢谢各位高手了。
            } 
      

  2.   

    System.Threading.Thread imputThread; 
    private void Button1_Click(object sender, System.EventArgs e)
    {
    System.Threading.Thread imputThread = new System.Threading.Thread(new System.Threading.ThreadStart(ImputData)); 
    imputThread.Start(); 
    }private void Button2_Click(object sender, System.EventArgs e)
    {
    imputThread.Abort();
    }
      

  3.   

    Thread imputThread =null;
    private void bootton1_Click(object sender, EventArgs e) 

                imputThread = new Thread(new ThreadStart(ImputData)); 
                imputThread.Start(); 
    } private void bootton2_Click(object sender, EventArgs e) 
    {
    if(imputThread!=null)
       {
         imputThread.Abort();
         imputThread=null;
       }
    }这样吧
      

  4.   

    System.Threading.Thread imputThread;         
    private void Button1_Click(object sender, System.EventArgs e)
    {
        imputThread = new System.Threading.Thread(new System.Threading.ThreadStart(ImputData)); 
        imputThread.Start(); 
    }private void Button2_Click(object sender, System.EventArgs e)
    {
        imputThread.Abort();
    }
      

  5.   

    Thread imputThread = null;private void bootton1_Click(object sender, EventArgs e)
            {
                if (imputThread == null)
                {
                   mputThread = new Thread(new ThreadStart(ImputData));
                }
                imputThread.Start();
            } 
    private void bootton2_Click(object sender, EventArgs e)
            {
                if(mputThread.IsAlive)
                   imputThread.Abort();
            } 
      

  6.   

    Abort()会引发异常.注意捕获他.
      

  7.   

    可以通过调用 Abort 来从一个线程终止另一个线程,但这会强行终止受影响的线程,而不管它是否已完成自己的任务,并且不提供清理资源的机会
      

  8.   

    最好的解决方案如下例所示(可参考msdn)using System;
    using System.Threading;public class Worker
    {
        // This method will be called when the thread is started.
        public void DoWork()
        {
            while (!_shouldStop)
            {
                Console.WriteLine("worker thread: working...");
            }
            Console.WriteLine("worker thread: terminating gracefully.");
        }
        public void RequestStop()
        {
            _shouldStop = true;
        }
        // Volatile is used as hint to the compiler that this data
        // member will be accessed by multiple threads.
        private volatile bool _shouldStop;
    }public class WorkerThreadExample
    {
        static void Main()
        {
            // Create the thread object. This does not start the thread.
            Worker workerObject = new Worker();
            Thread workerThread = new Thread(workerObject.DoWork);        // Start the worker thread.
            workerThread.Start();
            Console.WriteLine("main thread: Starting worker thread...");        // Loop until worker thread activates.
            while (!workerThread.IsAlive);        // Put the main thread to sleep for 1 millisecond to
            // allow the worker thread to do some work:
            Thread.Sleep(1);        // Request that the worker thread stop itself:
            workerObject.RequestStop();        // Use the Join method to block the current thread 
            // until the object's thread terminates.
            workerThread.Join();
            Console.WriteLine("main thread: Worker thread has terminated.");
        }
    }
      

  9.   

    因为是在不同方法里进行建立线程和结束线程的操作的,所以无论怎样都需要将建立线程的相关信息保存下来才能在另一个方法里结束它。因此至少需要一个成员变量来保存线程对象或者至少线程号之类的东西……然后结束按钮里只要通过那个对象获得线程,调用abort就行了吧……大体就是这样了,具体代码楼上各位好手都给了不少,参考着写应该就可以了吧……
      

  10.   

    if(imputThread!=null) 
      { 
        imputThread.Abort(); 
        imputThread=null; 
      } 
      

  11.   

    具体:
    private void bootton1_Click(object sender, EventArgs e) 
            { 
                Thread imputThread = new Thread(new ThreadStart(ImputData)); 
                imputThread.Start(); 
            } 如何在函数ImputData()中关闭进程imputThread呢?谢谢各位,此问题解决马上给分。ImputData()
    {
    //如何关闭正在运行的进程呢?
    }
      

  12.   

    System.Threading.Thread imputThread放到外面去就可以了。
    ImputData() 函数中用Thread.CurrentThread就可以当前线程了
      

  13.   

    原理很简单,将线程的委托函数的主要执行代码写入到while循环中。while(全局bool变量){执行语句;
    }
    在需要终止线程的时候,将该全局bool变量置为false就可以正常终止线程并不会有任何意外,因为一个线程的回调函数执行结束就标志该线程正常退出,这个应是一个标准的做法了。这样做的优点是可以正确的获得线程执行后的相应结果,而不必担心中途强制结束线程带来的各种问题。
      

  14.   

    原理很简单,将线程的委托函数的主要执行代码写入到while循环中。while(全局bool变量){执行语句;
    }
    在需要终止线程的时候,将该全局bool变量置为false就可以正常终止线程并不会有任何意外,因为一个线程的回调函数执行结束就标志该线程正常退出,这个应是一个标准的做法了。这样做的优点是可以正确的获得线程执行后的相应结果,而不必担心中途强制结束线程带来的各种问题。
      

  15.   

    原理很简单,将线程的委托函数的主要执行代码写入到while循环中。while(全局bool变量){执行语句;
    }
    在需要终止线程的时候,将该全局bool变量置为false就可以正常终止线程并不会有任何意外,因为一个线程的回调函数执行结束就标志该线程正常退出,这个应是一个标准的做法了。这样做的优点是可以正确的获得线程执行后的相应结果,而不必担心中途强制结束线程带来的各种问题。
      

  16.   

    Thread.CurrentThread,就是这个……获取到当前线程后就可以结束了……