多线程显示运行状态
最近碰見一個(gè)例子,Copy大文件或者網(wǎng)絡(luò)訪問的時(shí)候處理假死。 那就用多線程加個(gè)進(jìn)度條(只顯示運(yùn)行,沒有進(jìn)度)來表示狀態(tài)運(yùn)行吧。好了,廢話少說,上個(gè)例子。先看結(jié)果圖:
程序說明:
點(diǎn)擊Button,運(yùn)行一個(gè)數(shù)據(jù)累加器,textBox顯示每次運(yùn)行的結(jié)果,ProgressBar表示運(yùn)行的狀態(tài)。
好了,直接貼代碼:
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 Testpro
{public partial class Form1 : Form{BackgroundWorker work = new BackgroundWorker();public Form1(){InitializeComponent();work.WorkerReportsProgress = true;work.DoWork += Count;work.RunWorkerCompleted += completeRun;Control.CheckForIllegalCrossThreadCalls = false;this.textBox1.ScrollBars = ScrollBars.Both;}private void button1_Click(object sender, EventArgs e){this.progressBar1.Style = ProgressBarStyle.Marquee;work.RunWorkerAsync();}private void Count(object sender, DoWorkEventArgs e){ float num = 0.0f;int cun = 0;while (num < 5000){cun++;num += 4f; this.textBox1.Text += num.ToString() + "\t" + e.Result;if (cun == 9){textBox1.Text += Environment.NewLine;cun = 0;}} }public void SetText(object num){textBox1.Text += num.ToString() + "\n";}private void completeRun(object sender, RunWorkerCompletedEventArgs e){this.progressBar1.Style = ProgressBarStyle.Blocks;this.textBox1.Text += "complete";MessageBox.Show("Running complete.....");} }
}
轉(zhuǎn)載于:https://www.cnblogs.com/jimson/archive/2010/10/19/ThreadBar.html
總結(jié)
- 上一篇: 请问一下奥迪刷ECU动力升级是怎么收费的
- 下一篇: 超源和冰雪女皇哪个好