博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BackgroundWorker源码
阅读量:6312 次
发布时间:2019-06-22

本文共 8148 字,大约阅读时间需要 27 分钟。

//------------------------------------------------------------------------------// 
// Copyright (c) Microsoft Corporation. All rights reserved.//
//------------------------------------------------------------------------------namespace System.ComponentModel { using System.ComponentModel.Design.Serialization; using System.Diagnostics; using System.Security.Permissions; using System.Threading; [ SRDescription(SR.BackgroundWorker_Desc), DefaultEvent("DoWork"), HostProtection(SharedState = true) ] public class BackgroundWorker : Component { // Private statics private static readonly object doWorkKey = new object(); private static readonly object runWorkerCompletedKey = new object(); private static readonly object progressChangedKey = new object(); // Private instance members private bool canCancelWorker = false; private bool workerReportsProgress = false; private bool cancellationPending = false; private bool isRunning = false; private AsyncOperation asyncOperation = null; private readonly WorkerThreadStartDelegate threadStart; private readonly SendOrPostCallback operationCompleted; private readonly SendOrPostCallback progressReporter; public BackgroundWorker() { threadStart = new WorkerThreadStartDelegate(WorkerThreadStart); operationCompleted = new SendOrPostCallback(AsyncOperationCompleted); progressReporter = new SendOrPostCallback(ProgressReporter); } private void AsyncOperationCompleted(object arg) { isRunning = false; cancellationPending = false; OnRunWorkerCompleted((RunWorkerCompletedEventArgs)arg); } [ Browsable(false), SRDescription(SR.BackgroundWorker_CancellationPending) ] public bool CancellationPending { get { return cancellationPending; } } public void CancelAsync() { if (!WorkerSupportsCancellation) { throw new InvalidOperationException(SR.GetString(SR.BackgroundWorker_WorkerDoesntSupportCancellation)); } cancellationPending = true; } [ SRCategory(SR.PropertyCategoryAsynchronous), SRDescription(SR.BackgroundWorker_DoWork) ] public event DoWorkEventHandler DoWork { add { this.Events.AddHandler(doWorkKey, value); } remove { this.Events.RemoveHandler(doWorkKey, value); } } ///
[ Browsable(false), SRDescription(SR.BackgroundWorker_IsBusy) ] public bool IsBusy { get { return isRunning; } } ///
protected virtual void OnDoWork(DoWorkEventArgs e) { DoWorkEventHandler handler = (DoWorkEventHandler)(Events[doWorkKey]); if (handler != null) { handler(this, e); } } ///
protected virtual void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e) { RunWorkerCompletedEventHandler handler = (RunWorkerCompletedEventHandler)(Events[runWorkerCompletedKey]); if (handler != null) { handler(this, e); } } protected virtual void OnProgressChanged(ProgressChangedEventArgs e) { ProgressChangedEventHandler handler = (ProgressChangedEventHandler)(Events[progressChangedKey]); if (handler != null) { handler(this, e); } } [ SRCategory(SR.PropertyCategoryAsynchronous), SRDescription(SR.BackgroundWorker_ProgressChanged) ] public event ProgressChangedEventHandler ProgressChanged { add { this.Events.AddHandler(progressChangedKey, value); } remove { this.Events.RemoveHandler(progressChangedKey, value); } } // Gets invoked through the AsyncOperation on the proper thread. private void ProgressReporter(object arg) { OnProgressChanged((ProgressChangedEventArgs)arg); } // Cause progress update to be posted through current AsyncOperation. public void ReportProgress(int percentProgress) { ReportProgress(percentProgress, null); } // Cause progress update to be posted through current AsyncOperation. public void ReportProgress(int percentProgress, object userState) { if (!WorkerReportsProgress) { throw new InvalidOperationException(SR.GetString(SR.BackgroundWorker_WorkerDoesntReportProgress)); } ProgressChangedEventArgs args = new ProgressChangedEventArgs(percentProgress, userState); if (asyncOperation != null) { asyncOperation.Post(progressReporter, args); } else { progressReporter(args); } } public void RunWorkerAsync() { RunWorkerAsync(null); } public void RunWorkerAsync(object argument) { if (isRunning) { throw new InvalidOperationException(SR.GetString(SR.BackgroundWorker_WorkerAlreadyRunning)); } isRunning = true; cancellationPending = false; asyncOperation = AsyncOperationManager.CreateOperation(null); threadStart.BeginInvoke(argument, null, null); } [ SRCategory(SR.PropertyCategoryAsynchronous), SRDescription(SR.BackgroundWorker_RunWorkerCompleted) ] public event RunWorkerCompletedEventHandler RunWorkerCompleted { add { this.Events.AddHandler(runWorkerCompletedKey, value); } remove { this.Events.RemoveHandler(runWorkerCompletedKey, value); } } [ SRCategory(SR.PropertyCategoryAsynchronous), SRDescription(SR.BackgroundWorker_WorkerReportsProgress), DefaultValue(false) ] public bool WorkerReportsProgress { get { return workerReportsProgress; } set { workerReportsProgress = value; } } [ SRCategory(SR.PropertyCategoryAsynchronous), SRDescription(SR.BackgroundWorker_WorkerSupportsCancellation), DefaultValue(false) ] public bool WorkerSupportsCancellation { get { return canCancelWorker; } set { canCancelWorker = value; } } private delegate void WorkerThreadStartDelegate(object argument); private void WorkerThreadStart(object argument) { object workerResult = null; Exception error = null; bool cancelled = false; try { DoWorkEventArgs doWorkArgs = new DoWorkEventArgs(argument); OnDoWork(doWorkArgs); if (doWorkArgs.Cancel) { cancelled = true; } else { workerResult = doWorkArgs.Result; } } catch (Exception exception) { error = exception; } RunWorkerCompletedEventArgs e = new RunWorkerCompletedEventArgs(workerResult, error, cancelled); asyncOperation.PostOperationCompleted(operationCompleted, e); } }}

 

转载于:https://www.cnblogs.com/zjgtlkj/p/3752571.html

你可能感兴趣的文章
判断一个数字是否为素数的基于C语言的算法
查看>>
percona-toolkit工具检查mysql复制一致性及修复不一致性
查看>>
uC/OS-II源码分析(二)
查看>>
为什么连接字符串一定要用StringBuilder(介绍CLR Profiler)
查看>>
bootstrap2
查看>>
一张图让你学会Python
查看>>
nginx php动态编译加载模块.
查看>>
第5.6单元作业
查看>>
第九单元练习
查看>>
python3 做cgi 中文乱码问题
查看>>
Linux 关于Transparent Hugepages的介绍
查看>>
full decommisson of GDC
查看>>
go 与mysql
查看>>
阿里云正式推出航空大脑,帮助机场提高航班周转效率
查看>>
VMware Workstation :The VMware Authorization Service is not running.
查看>>
MySQL进程常见的State【转】
查看>>
不再局限于人脸识别,北大团队开发出“车脸”识别技术
查看>>
Java线程池
查看>>
Java防盗链在报表中的应用实例
查看>>
7.2bash 脚本选项及组合条件测试
查看>>