チェックポイントを短縮する方法はありますか?BackgroundWorker.CancellationPending
たとえば、return
以下のサンプルコードのようにカプセル化する方法はありますか?:
//REAL CODE (CURRENTLY USE THIS)
if (this.TW.CancellationPending)
return;
//PSEUDO REPLACEMENT CODE
this.CkPt(CurrentMethod); //PSEUDO USAGE
//^^^ PARAMETER IS A REFERENCE TO THE CURRENT METHOD, SIMILAR TO `this` FOR AN OBJECT
//OR MAYBE AN EXTENSION METHOD WOULD LOOK CLEANER
CurrentMethod.CkPt(); //PSEUDO USAGE
private void CkPt(Method m) //PSEUDO METHOD
{
/*
POSSIBLY PERFORM OTHER CHECKPOINT TASKS HERE
*/
if (this.TW.CancellationPending)
m.return/*FROM METHOD THAT CALLED ME*/;
}
私はこのようなマルチチェックポイントの状況をより読みやすくしようとしています:
//PSUEDO METHOD
//DO NOT TAKE THIS AS REPEATING CODE
//IT IS ONLY MEANT TO SHOW MULTIPLE USES OF THE SAME CHECKPOINT
//MY REAL TASK METHOD(S) CONTAIN MANY MANY MANY AREAS THAT DON'T REPEAT AND REQUIRE CHECKPOINTS
public void FakeBWTask()
{
if (this.TW.CancellationPending)
return;
foreach (var F1 in Fake1)
{
if (this.TW.CancellationPending)
return;
foreach (var F2 in Fake2)
{
if (this.TW.CancellationPending)
return;
foreach (var F3 in Fake3)
{
if (this.TW.CancellationPending)
return;
}
}
}
}
助けてくれてありがとう!