日期:2014-05-18 浏览次数:20822 次
/// <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(); }