當執行的任務需要很長時間時,要用到多線程,在新線程中執行任務。
有兩種方法:
一、使用Thread類
Thread workThread=new Thread(new ThreadStart(WorkMethod));
wordThread.start();
這樣,就在后臺執行WorkMethod方法了。
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 CShapApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int seconds;
private void btnButton_Click(object sender, EventArgs e)
{
seconds = Convert.ToInt32(this.textBox1.Text);
Thread workThread = new Thread(new ThreadStart(CountingDown));
workThread.Start();
}
private void CountingDown()
{
this.progressBar1.Maximum = seconds;
this.progressBar1.Minimum = 0;
this.progressBar1.Value = 0;
this.progressBar1.Step = 1;
for (int i = 0; i < seconds; i++)
{
Thread.Sleep(25);
this.progressBar1.PerformStep();
this.progressBar1.Refresh();
}
}
}
}
但是,由于ThreadStart是委托,不能帶參數。因此,如果需要參數,可以考慮下面的方法
二、通過beginInvoke。
由于beginInvoke是異步調用,因此可以在后臺運行。而begininvoke調用時還可以帶參數。
BeginInvoke(Delegate,object[]) ,object[]就是參數
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 CShapApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private delegate void WorkDelegate(int seconds);
private delegate void RefreshProgressBarDelegate();
private void btnButton_Click(object sender, EventArgs e)
{
Thread curThread = Thread.CurrentThread;
curThread.Name = "UI thread";
int seconds = Convert.ToInt32(this.textBox1.Text);
WorkDelegate workDelegate = new WorkDelegate(CountingDown);
workDelegate.BeginInvoke(seconds, null, null);
//Thread workThread = new Thread(new ThreadStart(CountingDown));
//workThread.Start();
}
private void CountingDown(int seconds)
{
MessageBox.Show(Thread.CurrentThread.Name);
this.progressBar1.Maximum = seconds;
this.progressBar1.Minimum = 0;
this.progressBar1.Value = 0;
this.progressBar1.Step = 1;
RefreshProgressBarDelegate refreshProgressBarDelegate = new RefreshProgressBarDelegate(RefreshProgressBar);
for (int i = 0; i < seconds; i++)
{
Thread.Sleep(250);
this.progressBar1.Invoke(refreshProgressBarDelegate);
}
}
private void RefreshProgressBar()
{
this.progressBar1.PerformStep();
this.progressBar1.Refresh();
}
}
}
轉自:
在我們應用程序開發過程中,經常會遇到一些問題,需要使用多線程技術來加以解決。本文就是通過幾個示例程序給大家講解一下多線程相關的一些主要問題。
執行長任務操作
許多種類的應用程序都需要長時間操作,比如:執行一個打印任務,請求一個 Web Service 調用等。用戶在這種情況下一般會去轉移做其他事情來等待任務的完成,同時還希望隨時可以監控任務的執行進度。
?/P>

下面的代碼片斷示例了當長任務執行時用戶界面是如何被更新的。
// 顯示進度條
void ShowProgress( int totalStep, int currentStep )
{
_Progress.Maximum = totalStep;
_Progress.Value = currentStep;
}

// 執行任務
void RunTask( int seconds )
{
// 每 1 / 4 秒 顯示進度一次
for( int i = 0; i < seconds * 4; i++ )
{
Thread.Sleep( 250 );

// 顯示進度條
ShowProgress( seconds * 4, i + 1 );
}
}

private void _btnRun_Click( object sender, System.EventArgs e )
{
RunTask( Convert.ToInt32( _txtSecond.Value ) );
}
當我們運行上面的程序,在整個長任務的過程中,沒有出現任何問題。這樣就真的沒有問題了嗎?當我們切換應用程序去做其他事情后再切換回來,問題就發生了!主窗體就會出現如下情況:

這個問題當然會發生,因為我們現在的應用程序是單線程的,因此,當線程執行長任務時,它同時也就不能重畫用戶界面了。
為什么在我們切換應用程序后,問題才發生呢?這是因為當你切換當前應用程序到后臺再切換回前臺時,我們需要重畫整個用戶界面。但是應用程序正在執行長任務,根本沒有時間處理用戶界面的重畫,問題就會發生。
如何解決問題呢?我們需要將長任務放在后臺運行,把用戶界面線程解放出來,因此我們需要另外一個線程。
線程異步操作
我們上面程序中執行按鈕的Click 處理如下:
private void _btnRun_Click( object sender, System.EventArgs e )
{
RunTask( Convert.ToInt32( _txtSecond.Value ) );
}
回想上面剛才問題發生的原因,直到 RunTask 執行完成后返回,Click 處理函數始終不能夠返回,這就意味著用戶界面不能處理重畫事件或其他任何事件。一個解決方法就是創建另外一個線程,代碼片斷如下:
using System.Threading;

private int _seconds;

// 執行任務工作線程進入點
void RunTaskThreadStart()
{
RunTask( _seconds );
}

// 通過創建工作線程消除用戶界面線程的阻塞問題
private void _btnRun_Click( object sender, System.EventArgs e )
{
_seconds = Convert.ToInt32( _txtSecond.Value );

Thread runTaskThread = new Thread( new ThreadStart( RunTaskThreadStart ) );

runTaskThread.Start();
}
現在,我們不再需要等待 RunTask 執行完成才能夠從 Click 事件返回,我們創建了新的工作線程并讓它開始工作、運行。

runTaskThread.Start(); 將我們新創建的工作線程調度執行并立即返回,允許我們的用戶界面線程重新獲得控制權執行它自己的工作。現在如果用戶再切換應用程序,因為工作線程在自己的 空間執行長任務,用戶界面線程被解放出來處理包括用戶界面重畫的各種事件,我們上面遇到的問題就解決了。
委托異步調用
在上面的代碼中,我們注意到,我們沒有給工作線程進入點(RunTaskThreadStart)傳遞任何參數,我們采用聲明一個窗體類的字段 _seconds 來給工作線程傳遞參數。在某種應用場合不能夠給工作線程直接傳遞參數也是一件非常痛苦的事情。
如何改進呢?我們可以使用委托來進行異步調用。委托是支持傳遞參數的。這樣,就消除了我們剛才的問題,使我們能夠消除額外的字段聲明和額外的工作線程函數。
如果你不熟悉委托,你可以簡單的把它理解為安全的函數指針。采用了委托異步調用,代碼片斷如下:
// 執行任務的委托聲明
delegate void RunTaskDelegate( int seconds );

// 通過創建委托解決傳遞參數問題
private void _btnRun_Click( object sender, System.EventArgs e )
{
RunTaskDelegate runTask = new RunTaskDelegate( RunTask );

// 委托同步調用方式
runTask( Convert.ToInt16( _txtSecond.Value ) );
}

//通過創建委托解決傳遞參數問題,通過委托的異步調用消除用戶界面線程的阻塞問題
private void _btnRun_Click( object sender, System.EventArgs e )
{
RunTaskDelegate runTask = new RunTaskDelegate( RunTask );

// 委托異步調用方式
runTask.BeginInvoke( Convert.ToInt16( _txtSecond.Value ), null, null );
}
多線程安全
到這里為止,我們已經解決了長任務的難題和傳遞參數的困擾。但是我們真的解決了全部問題嗎?回答是否定的。
我們知道 Windows 編程中有一個必須遵守的原則,那就是在一個窗體創建線程之外的任何線程中都不允許操作窗體。
我們上面的程序就是存在這樣的問題:工作線程是在 ShowProgress 方法中修改了用戶界面的進度條的屬性。那為什么程序運行沒有出現問題,運行正常呢?
沒有發生問題是因為是現在的Windows XP操作系統對這類問題有非常健壯的解決方法,讓我們避免了問題的發生。但是我們現在的程序不能保證在其他的操作系統能夠運行正常!
真正的解決方法是我們能夠認識到問題所在,并在程序中加以避免。

如何避免多線程的窗體資源訪問的安全問題呢?其實非常簡單,有兩種方法:
一種方法就是不管線程是否是用戶界面線程,對用戶界面資源的訪問統一由委托完成;
另一種方法是在每個 Windows Forms 用戶界面類中都有一個 InvokeRequired 屬性,它用來標識當前線程是否能夠直接訪問窗體資源。我們只需要檢查這個屬性的值,只有當允許直接訪問窗體資源時才直接訪問相應的資源,否則,就需要通過 委托進行訪問了。
采用第一種安全的方法的代碼片斷如下:
// 顯示進度條的委托聲明
delegate void ShowProgressDelegate( int totalStep, int currentStep );

// 顯示進度條
void ShowProgress( int totalStep, int currentStep )
{
_Progress.Maximum = totalStep;
_Progress.Value = currentStep;
}

// 執行任務的委托聲明
delegate void RunTaskDelegate( int seconds );

// 執行任務
void RunTask( int seconds )
{
ShowProgressDelegate showProgress = new ShowProgressDelegate( ShowProgress );

// 每 1 / 4 秒 顯示進度一次
for( int i = 0; i < seconds * 4; i++ )
{
Thread.Sleep( 250 );

// 顯示進度條
this.Invoke( showProgress, new object[] { seconds * 4, i + 1 } );
}
}


采用第二種安全的方法的代碼片斷如下:

// 顯示進度條的委托聲明
delegate void ShowProgressDelegate( int totalStep, int currentStep );

// 顯示進度條
void ShowProgress( int totalStep, int currentStep )
{
if( _Progress.InvokeRequired )
{
ShowProgressDelegate showProgress = new ShowProgressDelegate( ShowProgress );

// 為了避免工作線程被阻塞,采用異步調用委托
this.BeginInvoke( showProgress, new object[] { totalStep, currentStep } );
}
else
{
_Progress.Maximum = totalStep;
_Progress.Value = currentStep;
}
}

// 執行任務的委托聲明
delegate void RunTaskDelegate( int seconds );

// 執行任務
void RunTask( int seconds )
{
// 每 1 / 4 秒 顯示進度一次
for( int i = 0; i < seconds * 4; i++ )
{
Thread.Sleep( 250 );

// 顯示進度條
ShowProgress( seconds * 4, i + 1 );
}
}
至此,我們用了幾個示例說明了如何執行長任務、如何通過多線程異步處理任務進度的顯示并解決了多線程的安全性等問題。希望能夠給大家對理解多線程編程、委托的使用、異步調用等方面提供一些幫助,也希望能和大家進行進一步的溝通和交流。