我有这个代码逻辑来处理我的任务:
public abstract class BaseJob{
public bool Running { get; private set; }
public CancellationToken Token => _tokenSource.Token;
private CancellationTokenSource _tokenSource;
public virtual void Start()
{
_tokenSource = new CancellationTokenSource();
Running = true;
}
}
现在当我继承时,BaseJob我会遇到这种情况:
public class CustomJob: BaseJob{
public MyEnum Status {get; private set;}
public StopRequested { get; set;}
public override async void Start(){
base.Start();
while(!StopRequested){
await MyLongRunningMethod();
}
//base.Running = true but I can't access it here!
}
public async Task<bool> MyLongRunningMethod(){
await Task.Run(()=>{
//more work
},this.Token);
//someWork
return false;
}
}
如您所见,我无法在完成后Running立即将其设置为 false myJob,我想BaseJob处理它。
我必须对基类进行哪些代码逻辑更改才能自动检测何时myJob完成,然后在Running = false不访问继承类的最少访问的情况下完成任务?
慕尼黑的夜晚无繁华
慕莱坞森
相关分类