public Form1()
        {
            InitializeComponent();
            th = new Thread(new ThreadStart(run));
        }
        Thread th ;
        public void run()
        { 
            while (this.progressBar1.Value<=99)
            {
                this.progressBar1.Increment(1);
                Thread.Sleep(50);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            th.Start();
                
        }        private void button2_Click(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;
            th.Abort();
         }点开始   在点停止   在点开始就报错

解决方案 »

  1.   

    th = new Thread(new ThreadStart(run)); 
    后加一句:th.IsBackground = true;
      

  2.   

    你线程都已经abort了,怎么会再让你start???必须重新new thread才可以
      

  3.   

    而且建议不要这么做,stop用一个bool变量来控制,让线程自动结束,而不要abort,abort会导致资源可能释放不干净
      

  4.   

    this.progressBar1.Increment(1); 
    这里应该用Invoke.不然调试的时候会出错.
      

  5.   

    楼主的错误是不是这样的:线程间操作无效: 从不是创建控件“progressBar1”的线程访问它
    这样就行了,代码很拙劣
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    namespace 线程控制
    {
        public partial class Form1 : Form
        {
            Thread th;
            public Form1()
            {
                InitializeComponent();
                Control.CheckForIllegalCrossThreadCalls = false; //...            
            }        private void button1_Click(object sender, EventArgs e)
            {
                th = new Thread(new ThreadStart(run)); 
                th.Start();
                       }        private void button2_Click(object sender, EventArgs e)
            {
                this.progressBar1.Value = 0;
                th.Abort();
            }
            public void run()
            {
                while (this.progressBar1.Value <= 99)
                {
                    
                    this.progressBar1.Increment(1);
                    Thread.Sleep(50);
                }
            }     }
    }
    Windows 窗体控件本质上不是线程安全的。如果有两个或多个线程操作某一控件的状态,则可能会迫使该控件进入一种不一致的状态。还可能出现其他与线程相关的 bug,包括争用情况和死锁。确保以线程安全方式访问控件非常重要。 .NET Framework 有助于在以非线程安全方式访问控件时检测到这一问题。在调试器中运行应用程序时,如果创建某控件的线程之外的其他线程试图调用该控件,则调试器会引发一个 InvalidOperationException,并提示消息:“从不是创建控件 control name 的线程访问它。” 此异常在调试期间和运行时的某些情况下可靠地发生。强烈建议您在显示此错误信息时修复此问题。在调试以 .NET Framework 2.0 版之前的 .NET Framework 编写的应用程序时,可能会出现此异常。 可以通过将 CheckForIllegalCrossThreadCalls 属性的值设置为 false 来禁用此异常。
      

  6.   

    楼主误导别人 其实是this.progressBar1.Value <=99 跨线程操作了
    用BackgroundWorker组件吧 具体怎么用 MSDN