如果执行了Abort方法当然不能再执行Resume;
把TransThread.Resume();放在else里。

解决方案 »

  1.   

    楼上的理解错我的意思了:
    ThreadStart myStart=new ThreadStart(myFunc);
    Thread myThread=new Thread(myStart);
    myThread.Start();。
    这个线程是执行myFunc操作的。
    1。现在我有两个button,其中一个button1是开始操作,另外一个button2是执行取消操作的。当点击取消操作的时候,弹出“是否取消”messageBox,如果选择yes,就取消执行,如果选择no就继续执行。
    2。停止以后我点击button1就可以重新开始操作。请问这该怎么编码,谢谢了!!!!解决了以后再加分。
      

  2.   

    CancelButton的Onclick事件时对话窗代码,如果确认的话就执行线程的abort操作。如果你的线程是一个while,那么可以设置一个bool变量,每次while的头一句就判断这个变量,那么,如果这个变量为false(这里表示停止线程),那就退出function.
      

  3.   

    如果你的线程里面有对象的执行,那么最好把new的过程放到线程内部执行,否则,再次执行的时候很容易有对象未引用的异常发生。
      

  4.   

    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.ListBox listBox1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
             private static long i=0;
    private Thread t=null;

    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); t=new Thread(new ThreadStart(dowork)); } >
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {

    if(t.ThreadState==System.Threading.ThreadState.Suspended)
    {
    t.Resume();
    }
    else if (t.ThreadState==System.Threading.ThreadState.Unstarted)
    {

    t.Start();
    }

    } private void dowork()
    {

    while(true)
    {

                      this.listBox1.Items.Add(i.ToString());
    Application.DoEvents();
    if(i==long.MaxValue)
    i=0;
    else
    i++;
    Application.DoEvents();
    }
    }
     
    //stop the thread
    private void button2_Click(object sender, System.EventArgs e)
    {
       if(MessageBox.Show("are you sure that suspend the thread","one",System.Windows.Forms.MessageBoxButtons.OKCancel)==DialogResult.OK)
    t.Suspend();

    }
    }
      

  5.   

    看这样行不行:
    //开始按钮
    private void button1_Click(object sender, System.EventArgs e)
    {
    t=new Thread(new ThreadStart(dowork));
    if(t.ThreadState==System.Threading.ThreadState.Suspended)
    {
    t.Resume();
    }
    else if (t.ThreadState==System.Threading.ThreadState.Unstarted)
    {

    t.Start();
    }//停止按钮
    //stop the thread
    private void button2_Click(object sender, System.EventArgs e)
    {
         t.Suspend();
    if(MessageBox.Show("are you sure that suspend the thread","one",System.Windows.Forms.MessageBoxButtons.OKCancel)==DialogResult.OK)
    {
    t=null;
    }
    else
    t.Resume();
      

  6.   

    if(MessageBox.Show("are you sure that suspend the thread","one",System.Windows.Forms.MessageBoxButtons.OKCancel)==DialogResult.OK)
    {
    t=null;
    }谢谢楼上的,要的就是上面这个。原来付null 就解决问题了。-----------------------------------------------------------------------------------------
    还有一个问题,如果线程的方法正在执行中,我关闭了窗口,那该进程在资源管理器里面还存在,该怎么解决这个问题啊?
    我这样写,还是不行:
    private void Form1_Closed(object sender,System.EventArgs e)
    {
       t=null;
    this.Dispose();
    }
      

  7.   

    private void Form1_Closed(object sender, System.EventArgs e)
    {
    if(t!=null)
    {
    if(t.IsAlive)
    {
    t.Abort();
    }
    }
    }