VS 2008环境错误提示:线程正在运行或被终止;它无法重新启动。 搞不清楚……代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;using System.Threading;namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private Thread thread;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
thread = new Thread(new ThreadStart(fun)); Thread t = new Thread(new ThreadStart(a));
t.Start();
} private void button1_Click(object sender, EventArgs e)
{
//thread = new Thread(new ThreadStart(fun));
label1.Text = "0";
thread.Start();
button1.Enabled=false;
button2.Enabled=true;
button3.Enabled=false;
button4.Enabled=true;
} private void button2_Click(object sender, EventArgs e)
{
thread.Suspend();
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = true;
button4.Enabled = false; } private void button3_Click(object sender, EventArgs e)
{
thread.Resume();
button1.Enabled = false;
button2.Enabled = true;
button3.Enabled = false;
button4.Enabled = true; } private void button4_Click(object sender, EventArgs e)
{
if (thread.IsAlive)
{
thread.Abort();//撤销线程对象
button1.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
} } public void fun()
{
while(true)//死循环,线程将一直运行
{
int x = Convert.ToInt16(label1.Text);
x++;
label1.Text=Convert.ToString(x);
Thread.Sleep(1000);//休眠1秒钟,休眠一次,线程运行了1秒钟
} }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;using System.Threading;namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private Thread thread;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
thread = new Thread(new ThreadStart(fun)); Thread t = new Thread(new ThreadStart(a));
t.Start();
} private void button1_Click(object sender, EventArgs e)
{
//thread = new Thread(new ThreadStart(fun));
label1.Text = "0";
thread.Start();
button1.Enabled=false;
button2.Enabled=true;
button3.Enabled=false;
button4.Enabled=true;
} private void button2_Click(object sender, EventArgs e)
{
thread.Suspend();
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = true;
button4.Enabled = false; } private void button3_Click(object sender, EventArgs e)
{
thread.Resume();
button1.Enabled = false;
button2.Enabled = true;
button3.Enabled = false;
button4.Enabled = true; } private void button4_Click(object sender, EventArgs e)
{
if (thread.IsAlive)
{
thread.Abort();//撤销线程对象
button1.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
} } public void fun()
{
while(true)//死循环,线程将一直运行
{
int x = Convert.ToInt16(label1.Text);
x++;
label1.Text=Convert.ToString(x);
Thread.Sleep(1000);//休眠1秒钟,休眠一次,线程运行了1秒钟
} }
}
}
{
Control.CheckForIllegalCrossThreadCalls = false;
thread = new Thread(new ThreadStart(fun)); Thread t = new Thread(new ThreadStart(a));
t.Start();
}
改成
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false; Thread t = new Thread(new ThreadStart(a));
t.Start();
}------------
thread = new Thread(new ThreadStart(fun));写到这里,就好了……
-----------
private void button1_Click(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(fun));
label1.Text = "0";
thread.Start();
button1.Enabled=false;
button2.Enabled=true;
button3.Enabled=false;
button4.Enabled=true;
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}执行 CreateHandle() 时无法调用值 Dispose()。是下面这个原因么?
当您在 Visual Studio 调试器中运行代码时,如果从一个线程访问某个 UI 元素,而该线程不是创建该 UI 元素时所在的线程,则会引发 InvalidOperationException。调试器引发此异常以警告您存在危险的编程做法。UI 元素不是线程安全的,所以只应在创建它们的线程上进行访问。