楼主oldhunter(老猎人) 2006-04-23 23:43:34 在 .NET技术 / 组件/控件 开发 提问
用过2.0中BackgroundWorker的网友估计都有遭遇不爽的经历,网上有文章说已经被定为BUG了.无BUG版本估计要等.NET的下一个build了.
前两天写了个BackgroundWorker的替代者,我在自己的软件中已经用上了,
目前
没有发现问题.
用法与BackgroundWorker一样,除了一个InvokerControl属性. 此属性主要用在更新FORM时,把状态更新
方法
的执行由线程池转到调用线程中.估计用过多线程的都知道,这里就不多说了.
其它用法请参阅MDSN的BackgroundWorker类,里面的属性,方法及事件等与BackgroundWorker一模一样,所以可以用作过度替代,等.NET的build出来了再换过来:)
另外,如哪位可以改进一下,也请贴了来与大家共享! 例如能不能实现不用InvokerControl而可以直接将状态更新方法的执行转移到调用线程.
最后,如果大家喜欢请UP一下,使更多的人有
机会
参与讨论!
我的信箱:yrd#firsoft.com
qq:15630515
using System;
using System.Threading;
using System.ComponentModel;
using System.Windows.Forms;
namespace bget
{
class BackgroundWorker2 : Component
{
#region properties
private Control invokerControl;
public Control InvokerControl
{
get { return invokerControl; }
set { invokerControl = value; }
}
private bool cancellationPending;
public bool CancellationPending
{
get { return cancellationPending; }
}
private bool isBusy;
public bool IsBusy
{
get { return isBusy; }
}
private bool workerReportsProgress;
public bool WorkerReportsProgress
{
get { return workerReportsProgress; }
set { workerReportsProgress = value; }
}
private bool workerSupportsCancellation;
public bool WorkerSupportsCancellation
{
get { return workerSupportsCancellation; }
set { workerSupportsCancellation = value; }
}
#endregion
public void RunWorkerAsync()
{
RunWorkerAsync(null);
}
public void RunWorkerAsync(Object argument)
{
if (IsBusy)
{
throw new InvalidOperationException("Worker is busy now.");
}
OnDoWork(new DoWorkEventArgs(argument));
}
public void ReportProgress(int percentProgress)
{
ReportProgress(percentProgress, null);
}
public void ReportProgress(int percentProgress, Object userState)
{
if ( !workerReportsProgress )
{
throw new InvalidOperationException("WorkerReportsProgress is required to be true.");
}
OnProgressChanged(new ProgressChangedEventArgs(percentProgress, userState));
}
public void CancelAsync()
{
if ( !workerSupportsCancellation )
{
throw new InvalidOperationException("WorkerSupportsCancellation is required to be true.");
}
cancellationPending = true;
}
#region events
public event DoWorkEventHandler DoWork;
protected virtual void OnDoWork(DoWorkEventArgs e)
{
if (DoWork != null)
{
isBusy = true;
try
{
DoWork.BeginInvoke(this, e, new AsyncCallback(DoWorkCallback), e); // 这里使用的是线程池
}
catch (Exception ex)
{
OnRunWorkerCompleted(new RunWorkerCompletedEventArgs(null, ex, false));
isBusy = false;
cancellationPending = false;
}
}
}
private void DoWorkCallback(IAsyncResult ar)
{
if (ar.IsCompleted)
{
DoWorkEventArgs args = ar.AsyncState as DoWorkEventArgs;
if (args != null)
{
OnRunWorkerCompleted(new RunWorkerCompletedEventArgs(args.Result, null, args.Cancel));
}
isBusy = false;
cancellationPending = false;
}
}
public event ProgressChangedEventHandler ProgressChanged;
protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
{
if (ProgressChanged != null)
{
if (invokerControl == null)
{
throw new InvalidOperationException("InvokerControl is required.");
}
if (invokerControl.InvokeRequired)
{
invokerControl.Invoke(ProgressChanged, this, e);
}
else
{
ProgressChanged(this, e);
}
}
}
public event RunWorkerCompletedEventHandler RunWorkerCompleted;
protected virtual void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
{
if (RunWorkerCompleted != null)
{
if (invokerControl == null)
{
throw new InvalidOperationException("InvokerControl is required.");
}
if (invokerControl.InvokeRequired)
{
invokerControl.Invoke(RunWorkerCompleted, this, e);
}
else
{
RunWorkerCompleted(this, e);
}
}
}
#endregion
}
}