您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“C#中Backgroundworker與Thread的區(qū)別是什么”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
BackgroundWorker是微軟的在.net Framwork中添加的一個組件,主要對線程的訪問提供了一種安全的方式。簡單的說就是對Thread的一次封裝。
首先介紹一下BackgroundWorker的相關(guān)屬性和方法:
屬性:
WorkerReportsProgress:是否可以報告進度。
WorkerSupportsCancellation:是否允許異步中止。
IsBusy:是否在運行。
CancellationPending:判斷BackgroundWorker是否已經(jīng)異步取消。
方法:
RunWorkerAsync:開始執(zhí)行任務(wù)。觸發(fā)DoWork事件
ReportProgress:異步提醒,觸發(fā)ProgressChanged事件,但是這個如果可以使用,必須設(shè)置WorkerReportsProgress為True
CancelAsync:取消BackgroundWorker操作。
事件:
DoWork:執(zhí)行RunWorkerAsync后觸發(fā),異步執(zhí)行的認為。
ProgressChanged:執(zhí)行ReportProgress時觸發(fā),異步獲得進度。
RunWorkerCompleted:線程結(jié)束時觸發(fā),主要有成功結(jié)束,發(fā)生異?;蛘呷∠麜r發(fā)生。
一個簡單的例子:
public partial class MainWindow : Window { private BackgroundWorker m_BackgroundWorker;// 申明后臺對象 public MainWindow() { InitializeComponent(); m_BackgroundWorker = new BackgroundWorker(); // 實例化后臺對象 m_BackgroundWorker.WorkerReportsProgress = true; // 設(shè)置可以通告進度 m_BackgroundWorker.WorkerSupportsCancellation = true; // 設(shè)置可以取消 m_BackgroundWorker.DoWork += new DoWorkEventHandler(DoWork); m_BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(UpdateProgress); m_BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork); m_BackgroundWorker.RunWorkerAsync(this); } void DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = sender as BackgroundWorker; MainWindow win = e.Argument as MainWindow; int i = 0; while ( i <= 100 ) { if (bw.CancellationPending) { e.Cancel = true; break; } bw.ReportProgress(i++); Thread.Sleep(1000); } } void UpdateProgress(object sender, ProgressChangedEventArgs e) { int progress = e.ProgressPercentage; label1.Content = string.Format("{0}",progress); } void CompletedWork(object sender, RunWorkerCompletedEventArgs e) { if ( e.Error != null) { MessageBox.Show("Error"); } else if (e.Cancelled) { MessageBox.Show("Canceled"); } else { MessageBox.Show("Completed"); } } private void button1_Click(object sender, RoutedEventArgs e) { m_BackgroundWorker.CancelAsync(); } }
BackgroundWorker就是一個高級控件,方便使用Thread,后者是前者的靈魂或基礎(chǔ)
直接使用后者難度稍大,但換來的是靈活方便。
Thread的使用就比較麻煩了,對于尤其是對異步提醒來說,需要寫委托,代碼量是很多,但是對于BackgroundWorker來說,卻沒有線程暫停和繼續(xù)的方法。但是對于一般的來說,這些功能也是不用的,而且在微軟的文檔中還提到了,Thread的Resume和Suspend已經(jīng)不推薦使用。
一個簡單的例子:
using System; using System.Threading; namespace ThreadsComm { public delegate void ReadParamEventHandler(string sParam); class MyThread { public Thread thread1; private static ReadParamEventHandler OnReadParamEvent; public MyThread() { thread1 = new Thread(new ThreadStart(MyRead)); thread1.IsBackground = true; thread1.Start(); } public event ReadParamEventHandler ReadParam { add { OnReadParamEvent += new ReadParamEventHandler(value);} remove{ OnReadParamEvent -= new ReadParamEventHandler(value);} } protected void MyRead() { int i = 0; while (true) { Thread.Sleep(1000); i = i + 1; OnReadParamEvent(i.ToString());//觸發(fā)事件 } } } } using System; using System.Windows.Forms; namespace ThreadsComm { public partial class Form1 : Form { private static string param = ""; public Form1() { InitializeComponent(); MyThread thread1 = new MyThread(); thread1.ReadParam += this.OnRead; } private void OnRead(string sParam) { param = sParam; Object[] list = { this,System.EventArgs.Empty}; this.lblShow.BeginInvoke(new EventHandler(LabelShow), list); } protected void LabelShow(Object o, EventArgs e) { this.lblShow.Text = param; } } }
“C#中Backgroundworker與Thread的區(qū)別是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。