110,538
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 线程。
/// </summary>
private Thread myThread;
public Form1()
{
InitializeComponent();
// Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("这是主线程");
}
private void ThreadTask()
{
int stp, newVal;
Random random = new Random();
if (this.progressBar1.InvokeRequired)
{
mythis m = new mythis(ThreadTask);
this.progressBar1.Invoke(m);
}
while (true)
{
stp = this.progressBar1.Step * random.Next(-1, 2);
newVal = this.progressBar1.Value + stp;
if (newVal > this.progressBar1.Maximum)
newVal = this.progressBar1.Maximum;
else if (newVal < this.progressBar1.Minimum)
newVal = this.progressBar1.Minimum;
this.progressBar1.Value = newVal;
Thread.Sleep(100);
}
}
delegate void mythis();
private void Form1_Load(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(this.ThreadTask));
myThread.IsBackground = true;
myThread.Start();
}
private void step(){
int stp, newVal;
Random random = new Random();
if (this.InvokeRequired)
{
mythis m = new mythis(ThreadTask);
this.Invoke(m);
}else{
stp = this.progressBar1.Step * random.Next(-1, 2);
newVal = this.progressBar1.Value + stp;
if (newVal > this.progressBar1.Maximum)
newVal = this.progressBar1.Maximum;
else if (newVal < this.progressBar1.Minimum)
newVal = this.progressBar1.Minimum;
this.progressBar1.Value = newVal;
}
}
private void ThreadTask()
{
while (true)
{
step();
Thread.Sleep(100);
}
}