134 lines
4.0 KiB
C#
134 lines
4.0 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Threading;
|
|
|
|
namespace CoreUnit.Common
|
|
{
|
|
public class BackgroundWorkerUnit : IDisposable
|
|
{
|
|
delegate void UpdatePageCallBack();
|
|
|
|
public delegate void EventDoWork();
|
|
public EventDoWork evtDoWork;
|
|
|
|
protected BackgroundWorker m_backgroundWorker;
|
|
public BackgroundWorker backgroundWorker {
|
|
get { lock (this) { return m_backgroundWorker; } }
|
|
set { lock (this) { m_backgroundWorker = value; } }
|
|
}
|
|
|
|
int m_progressPercentage = 0;
|
|
public int ProgressPercentage {
|
|
get { lock (this) { return m_progressPercentage; } }
|
|
set { lock (this) { m_progressPercentage = value; } }
|
|
}
|
|
|
|
bool m_isDoWorkCompleted = false;
|
|
public bool IsDoWorkCompleted {
|
|
get { lock (this) { return m_isDoWorkCompleted; } }
|
|
set { lock (this) { m_isDoWorkCompleted = value; } }
|
|
}
|
|
|
|
System.Windows.Forms.Control m_ctrl = null;
|
|
|
|
System.Threading.Timer m_backgroundWorkerTimer = null;
|
|
int m_period = 0;
|
|
public int Period {
|
|
get { lock (this) { return m_period; } }
|
|
set { lock (this) { m_period = value; } }
|
|
}
|
|
|
|
public void StartBackgroundWorker()
|
|
{
|
|
m_backgroundWorkerTimer = new System.Threading.Timer(RunDoWork, null, 0, m_period);
|
|
}
|
|
|
|
public void StopBackgroundWorker()
|
|
{
|
|
if (m_backgroundWorkerTimer != null) {
|
|
m_backgroundWorkerTimer.Dispose();
|
|
}
|
|
}
|
|
|
|
public BackgroundWorkerUnit(System.Windows.Forms.Control ctrl, int period = 300)
|
|
{
|
|
m_ctrl = ctrl;
|
|
m_period = period;
|
|
m_backgroundWorker = new BackgroundWorker();
|
|
m_backgroundWorker.WorkerReportsProgress = true;
|
|
m_backgroundWorker.WorkerSupportsCancellation = true;
|
|
m_backgroundWorker.DoWork += new DoWorkEventHandler(DoWork);
|
|
m_backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
|
|
m_backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
|
|
}
|
|
|
|
//public void DoWork(object sender, DoWorkEventArgs e)
|
|
//{
|
|
|
|
// while (backgroundWorker.CancellationPending != true)
|
|
// {
|
|
// if (m_ctrl.InvokeRequired)
|
|
// {
|
|
// UpdatePageCallBack d = new UpdatePageCallBack(evtDoWork);
|
|
// m_ctrl.Invoke(d, new object[] { });
|
|
// }
|
|
// else
|
|
// {
|
|
// evtDoWork();
|
|
// }
|
|
|
|
// Thread.Sleep(m_period);
|
|
// }
|
|
|
|
//}
|
|
|
|
//public void ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|
//{
|
|
// ProgressPercentage = e.ProgressPercentage;
|
|
//}
|
|
public void ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|
{
|
|
evtDoWork();
|
|
}
|
|
|
|
public void DoWork(object sender, DoWorkEventArgs e)
|
|
{
|
|
while (m_backgroundWorker.CancellationPending != true) {
|
|
m_backgroundWorker.ReportProgress(0);
|
|
Thread.Sleep(Period);
|
|
}
|
|
|
|
e.Cancel = true;
|
|
}
|
|
private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|
{
|
|
if (e.Error != null) {
|
|
return;
|
|
}
|
|
|
|
IsDoWorkCompleted = true;
|
|
}
|
|
|
|
public void RunDoWork(object obj)
|
|
{
|
|
if (!backgroundWorker.IsBusy) {
|
|
IsDoWorkCompleted = false;
|
|
backgroundWorker.RunWorkerAsync();
|
|
}
|
|
}
|
|
|
|
public void StopDoWork()
|
|
{
|
|
if (backgroundWorker.CancellationPending != true) {
|
|
backgroundWorker.CancelAsync();
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
StopBackgroundWorker();
|
|
backgroundWorker.CancelAsync();
|
|
}
|
|
}
|
|
}
|