最近碰見一個例子,Copy大檔案或者網絡通路的時候處理假死。 那就用多線程加個進度條(隻顯示運作,沒有進度)來表示狀态運作吧。好了,廢話少說,上個例子。先看結果圖:
程式說明:
點選Button,運作一個資料累加器,textBox顯示每次運作的結果,ProgressBar表示運作的狀态。
好了,直接貼代碼:
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.....");
}
}
}
轉載于:https://www.cnblogs.com/jimson/archive/2010/10/19/ThreadBar.html