一、方法一:使用線程c#
功能描述:在用c#作WinFrom開發的過程當中。咱們常常須要用到進度條(ProgressBar)用於顯示進度信息。這時候咱們可能就須要用到多線程,若是不採用多線程控制進度條,窗口很容易假死(沒法適時看到進度信息)。下面我就簡單結合一個我寫的例子給你們作一個介紹。多線程
第一步:設計界面,注意須要引用 using System.Threading;函數
private delegate void SetPos(int ipos,string vinfo);//代理ui
第三步:進度條值更新函數(參數必須跟聲明的代理參數同樣)this
private void SetTextMesssage(int ipos,string vinfo)
{
if (this.InvokeRequired)
{
SetPos setpos = new SetPos(SetTextMesssage);
this.Invoke(setpos, new object[] { ipos,vinfo });
}
else
{
this.label1.Text = ipos.ToString() + "/1000";
this.progressBar1.Value = Convert.ToInt32(ipos);
this.textBox1.AppendText(vinfo);
}
}線程
第四步:函數實現設計
private void button1_Click(object sender, EventArgs e)
{
Thread fThread = new Thread(new ThreadStart(SleepT));
fThread.Start();
}代理
第五步:新的線程執行函數:orm
private void SleepT()
{
for (int i = 0; i < 500; i++)
{
System.Threading.Thread.Sleep(10);
SetTextMesssage(100*i/500,i.ToString()+"\r\n");
}
}事件
程序運行效果圖:
控件名稱:
button1;backgroundWorker1;
對backgroundWorker1控件,屬性設置:
第二步:主頁面後臺代碼:
using System.Threading;//引用空間名稱
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync(); // 運行 backgroundWorker 組件
ProcessForm form = new ProcessForm(this.backgroundWorker1);// 顯示進度條窗體
form.ShowDialog(this);
form.Close();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
}
else
{
}
}
//你能夠在這個方法內,實現你的調用,方法等。
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
worker.ReportProgress(i);
if (worker.CancellationPending) // 若是用戶取消則跳出處理數據代碼
{
e.Cancel = true;
break;
}
}
}
分別爲button控件和backgroundWorker1控件選好事件。
第三步:設置子窗體(及顯示進度條的窗體):
private BackgroundWorker backgroundWorker1; //ProcessForm 窗體事件(進度條窗體)
public ProcessForm(BackgroundWorker backgroundWorker1)
{
InitializeComponent();
this.backgroundWorker1 = backgroundWorker1;
this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//this.Close();//執行完以後,直接關閉頁面
}
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.CancelAsync();
this.button1.Enabled = false;
this.Close();
}
}
只爲button選好事件
執行效果爲:
=================================================================================================
三、第三種方法:
功能描述:在處理大量數據的時候,有時候方法的執行須要必定的時間,這時候每每會形成頁面或程序的「假死」狀態,給用戶的體驗度也不是很好。爲了不出現「假死」提升用戶的體驗度,在這裏爲這類型的方法加了一個進度條和一個文本框,進度條用於顯示程序處理的進度,文本框用於顯示在處理過程當中,給與的提示。本方法主要使用了控件:backgroundWorker1;說明:本方法與上面的方法(方法二)基本類型,主要是設計和代碼進行了一些修改。
第一步:主窗體設計:
控件名稱:
button1;backgroundWorker1;
對backgroundWorker1控件,屬性設置:
第二步:主窗體 後臺代碼:
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync(); // 運行 backgroundWorker 組件
ProcessForm form = new ProcessForm(this.backgroundWorker1);// 顯示進度條窗體
form.ShowDialog(this);
form.Close();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
}
else
{
}
}
//你能夠在這個方法內,實現你的調用,方法等。
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
worker.ReportProgress(i, i.ToString() + " 你好!\r\n"); //注意:這裏向子窗體返回信息值,這裏是兩個值,一個用於進度條,一個用於文本框的。
if (worker.CancellationPending) // 若是用戶取消則跳出處理數據代碼
{
e.Cancel = true;
break;
}
}
}
第三步:設置子窗體(及顯示進度條的窗體):
控件:
progressBar1;textBox1;button1
第四步:子窗體後臺代碼:
private BackgroundWorker backgroundWorker1; //ProcessForm 窗體事件(進度條窗體)
public ProcessForm(BackgroundWorker backgroundWorker1)
{
InitializeComponent();
this.backgroundWorker1 = backgroundWorker1;
this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//this.Close();//執行完以後,直接關閉頁面
}
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
this.textBox1.AppendText(e.UserState.ToString());//主窗體傳過來的值,經過e.UserState.ToString()來接受
}
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.CancelAsync();
this.button1.Enabled = false;
this.Close();
}
實現的效果: