0
暫停一個BackgroundWorker我有一個程序,被連續地運行。從另一個方法
當我啓動該程序,我有一個BackgroundWorker(InitialStart.cs):
BackgroundWorker recordingSyncWorker = new BackgroundWorker();
recordingSyncWorker.DoWork += new DoWorkEventHandler(RecordingSyncCheck);
recordingSyncWorker.WorkerSupportsCancellation = false;
recordingSyncWorker.WorkerReportsProgress = false;
recordingSyncWorker.RunWorkerAsync();
void RecordingSyncCheck(object sender, DoWorkEventArgs e)
{
cHandler ch = new cHandler();
Timer theTimer;
int seconds = 1;
if (taskSeconds != null && taskSeconds != "")
seconds = Convert.ToInt32(taskSeconds);
int milliseconds = seconds * 1000;
theTimer = new Timer(10000);
theTimer.Elapsed += new ElapsedEventHandler(ch.SyncMethod);
theTimer.Interval = milliseconds;
theTimer.Enabled = true;
}
而且我有兩種方法在另一個類(cHandler.cs):
SyncMethod(object source, System.Timers.ElapsedEventArgs e)
{
// random code
}
private string SDCardCheck(object whatever)
{
//more code
}
SDCardCheck得到稱爲通過WCF,所以它就像運行cHandler.cs的另一個實例。當我打電話給SDCardCheck時,有沒有辦法讓我暫停BackgroundWorker?
值得注意的是,要做到這一點,在'Timer'需要已經不僅僅是方法的範圍較廣。 – 2014-10-30 16:49:40
我禁用了BackgroundWorker的,剛跑了,直接從UI線程。初始測試看起來不錯。我認爲BackgroundWorker是以前版本的補充,因爲Syncing方法更加流暢。 – javisrk 2014-10-30 18:57:55