一个后台处理的类
//模仿BackgroudWorker写的类//实现了一部分比较实用的功能
//这个类仅仅用正则表达式匹配大文本,并返回匹配结果
//对处的事件有二个:
//JobComplete和JobFailed
//如果需要做其它工作可在DoJob()过程里修改
public delegate void JobCompleteHandle(System.Text.RegularExpressions.MatchCollection e);
public delegate void JobFailHandle(string e);
public delegate void StatusChangedHandle(bool e);
public class BGWorker
{
public event JobCompleteHandle JobComplete;
public event JobFailHandle JobFailed;
private event JobCompleteHandle JobCompletePretreat;
private event JobFailHandle JobFailedPretreat;
private event StatusChangedHandle StatusChanged;
string s_ToTreat = string.Empty;
string s_RegularString = string.Empty;
System.Text.RegularExpressions.RegexOptions ro_Option = System.Text.RegularExpressions.RegexOptions.Multiline;
bool blIsBusy = false;
System.Windows.Forms.Form frmOwner;
System.Threading.Thread thrdWorker;
//传递进来的Form主要作用就是使子线程能从主线引发JobComplete和JobFailed事件
public BGWorker(System.Windows.Forms.Form frmOwner)
{
this.frmOwner = frmOwner;
thrdWorker = CreateThread();
this.JobCompletePretreat += new JobCompleteHandle(BGWorker_JobCompletePretreat);
this.JobFailedPretreat += new JobFailHandle(BGWorker_JobFailedPretreat);
this.StatusChanged += new StatusChangedHandle(BGWorker_StatusChanged);
}
//切换线程
void BGWorker_JobFailedPretreat(string e)
{
if (frmOwner.InvokeRequired)
{
JobFailHandle jfhT = new JobFailHandle(BGWorker_JobFailedPretreat);
frmOwner.Invoke(jfhT, new object[] { e });
}
else
{
JobFailed(e);
}
}
//切换线程,如果没有的话,从外部取得的IsBusy将保持为False
void BGWorker_StatusChanged(bool e)
{
if (frmOwner.InvokeRequired)
{
StatusChangedHandle schT = new StatusChangedHandle(BGWorker_StatusChanged);
frmOwner.Invoke(schT, new object[] { e });
}
else
{
blIsBusy = e;
}
}
//切换线程
void BGWorker_JobCompletePretreat(System.Text.RegularExpressions.MatchCollection e)
{
if (frmOwner.InvokeRequired)
{
JobCompleteHandle jchT = new JobCompleteHandle(BGWorker_JobCompletePretreat);
frmOwner.Invoke(jchT, new object[] { e });
}
else
{
JobComplete(e);
}
}
public string sToTreat
{
set
{
s_ToTreat = value;
}
}
public string sRegularString
{
set
{
s_RegularString = value;
}
}
public System.Text.RegularExpressions.RegexOptions roOption
{
set
{
ro_Option = value;
}
}
public bool IsReady
{
get
{
return s_RegularString.Length > 0 && s_ToTreat.Length > 0;
}
}
public bool IsBusy
{
get
{
return blIsBusy;
}
}
public void StartJob()
{
if (thrdWorker.ThreadState != System.Threading.ThreadState.Unstarted)
{
thrdWorker.Abort();//做比较复杂的工作不建议直接Abort,这样容易导致数据丢失,但在这里面不怕丢失所以直接Abort
thrdWorker = CreateThread();
}
thrdWorker.Start();
}
public void StopJob()
{
if (thrdWorker.ThreadState == System.Threading.ThreadState.Running)
{
thrdWorker.Abort();
blIsBusy = false;
}
}
private System.Threading.Thread CreateThread()
{
System.Threading.Thread thrT = new System.Threading.Thread(new System.Threading.ThreadStart(DoJob));
thrT.Name = "WorkThread";
thrT.Priority = System.Threading.ThreadPriority.Lowest;
thrT.IsBackground = true;
return thrT;
}
void DoJob()
{
StatusChanged(true);
StringBuilder sbT = new StringBuilder();
try
{
System.Text.RegularExpressions.Regex reT = new System.Text.RegularExpressions.Regex(s_RegularString, ro_Option);
System.Text.RegularExpressions.MatchCollection mcT = reT.Matches(s_ToTreat);
JobCompletePretreat(mcT);
}
catch (Exception E)
{
JobFailedPretreat(E.Message);
}
StatusChanged(false);
}
}