主题:关于线程的简单问题(希望帮帮忙 o(∩_∩)o...)
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 WindowsApplication2
{
public partial class Form1 : Form
{
public Thread trd;//定义一个线程
private void ThreadTask()
{
int stp;
int newval;
Random rnd = new Random();
while (true)
{
stp = this.progressBar1.Step * rnd.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;
this.progressBar1.Value = newval;//就是这里的问题
Thread.Sleep(100);
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("this is main threading");
}
private void Form1_Load(object sender, EventArgs e)
{
Thread trd = new Thread(new ThreadStart(this.ThreadTask));
trd.IsBackground = true;
trd.Start();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Thread trd;//定义一个线程
private void ThreadTask()
{
int stp;
int newval;
Random rnd = new Random();
while (true)
{
stp = this.progressBar1.Step * rnd.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;
this.progressBar1.Value = newval;//就是这里的问题
Thread.Sleep(100);
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("this is main threading");
}
private void Form1_Load(object sender, EventArgs e)
{
Thread trd = new Thread(new ThreadStart(this.ThreadTask));
trd.IsBackground = true;
trd.Start();
}
}
}