TaskOperatorEnum isStartTask = TaskOperatorEnum.Finished;
public TaskOperatorEnum IsStartTask { get { return isStartTask; } set { isStartTask = value; RaisePropertyChanged(); } }
public ICommand ExecutedTask => new DelegateCommand<object>((obj) => {
var operateType = (TaskOperatorEnum)obj;
if (IsStartTask == TaskOperatorEnum.Finished || IsStartTask == TaskOperatorEnum.UnCreated)
{
if (selectedTask == null)
{
logger.LogInformation("请选择开始的任务");
return;
}
StartTask();
}
else if (IsStartTask == TaskOperatorEnum.Running)
{
IsStartTask = TaskOperatorEnum.Stop;
logger.LogInformation("暂停任务");
}
else if (IsStartTask == TaskOperatorEnum.Stop)
{
IsStartTask = TaskOperatorEnum.Running;
logger.LogInformation("继续任务");
}
});
public void StopTask()
{
}
public void ContinueTask()
{
}
public CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
public ManualResetEvent resetEvent= new ManualResetEvent(true);
Task EndExeucted;
/// <summary>
/// 开始的任务
/// </summary>
public void StartTask()
{
IsStartTask = TaskOperatorEnum.Running;
//取消任务的指令
cancellationTokenSource = new CancellationTokenSource();
//暂停任务的指令
resetEvent = new ManualResetEvent(true);
cancellationTokenSource.Token.Register(() => {
EndExeucted = Task.Run(() => { });
Task.WaitAny(EndExeucted);
logger.LogInformation("任务已经取消");
});
Task.Factory.StartNew(async () => {
await Exeucted(SelectedTask,true);
},cancellationTokenSource.Token);
}
public async Task Exeucted(TaskInfoDto taskInfoDto,bool isRun)
{
logger.LogInformation("任务开始执行");
await QueryAlltaskInfoAsync();
}
public ICommand EndTask => new DelegateCommand(() => {
IsStartTask = TaskOperatorEnum.Finished;
cancellationTokenSource.Cancel();
});
任务执行框架
最新推荐文章于 2024-04-03 09:52:20 发布