在我正在实施的自定义InvalidOperationException
上运行任务时,偶尔会收到以下TaskScheduler
。
在调查问题时,它似乎是Mono实施中的一个错误。当继续任务标有TaskContinuationOptions.ExecuteSynchronously
时,它将被传递给任务调度程序的TryExecuteTaskInline
方法;但是,如果后者拒绝执行它并返回false
,那么将不可避免地遇到以下异常(根据下面的代码摘录)。
有人可以建议在Mono上解决这个问题的方法吗?我正在考虑改变我的TryExecuteTaskInline
实现,以便它总是接受执行同步延续;但是,我还没有找到一种方法来确定任务是否是一个延续(不使用反射)。
System.InvalidOperationException: Start may not be called on a continuation task
at System.Threading.Tasks.Task.Start (System.Threading.Tasks.TaskScheduler scheduler) [0x00000] in <filename unknown>:0
at System.Threading.Tasks.Task.RunSynchronouslyCore (System.Threading.Tasks.TaskScheduler scheduler) [0x00000] in <filename unknown>:0
at System.Threading.Tasks.TaskContinuation.Execute () [0x00000] in <filename unknown>:0
at System.Threading.Tasks.Task.ProcessCompleteDelegates () [0x00000] in <filename unknown>:0
at System.Threading.Tasks.Task.Finish () [0x00000] in <filename unknown>:0
at System.Threading.Tasks.Task.ThreadStart () [0x00000] in <filename unknown>:0
at System.Threading.Tasks.Task.Execute () [0x00000] in <filename unknown>:0
at System.Threading.Tasks.TaskScheduler.TryExecuteTask (System.Threading.Tasks.Task task) [0x00000] in <filename unknown>:0
at System.Threading.Tasks.Schedulers.WorkStealingTaskScheduler.DispatchLoop (CancellationToken cancellationToken) [0x00000] in <filename unknown>:0
我在相关(精简)部分下面粘贴......
...来自TaskContinuation.cs
:
class TaskContinuation
{
public void Execute ()
{
// ...
if ((continuationOptions & TaskContinuationOptions.ExecuteSynchronously) != 0)
task.RunSynchronouslyCore (task.scheduler);
else
task.Schedule ();
}
}
...来自Task.cs
:
public class Task
{
internal void RunSynchronouslyCore(TaskScheduler scheduler)
{
// ...
if (scheduler.RunInline(this, false))
return;
Start(scheduler);
Wait();
}
public void Start(TaskScheduler scheduler)
{
// ...
if (IsContinuation)
throw new InvalidOperationException("Start may not be called on a continuation task");
SetupScheduler(scheduler);
Schedule();
}
}
...来自TaskScheduler.cs
:
public abstract class TaskScheduler
{
protected abstract bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued);
internal bool RunInline(Task task, bool taskWasPreviouslyQueued)
{
// ...
return TryExecuteTaskInline(task, taskWasPreviouslyQueued);
}
}
答案 0 :(得分:1)
修复起来很简单。唯一需要的更改是将https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Threading.Tasks/Task.cs#L228更改为调用方法Schedule而不是Start
答案 1 :(得分:0)
对于遇到此问题但被限制使用Mono的固定版本(包括Mono 3.0.12)的任何人,您可以使用以下hack。这是低效的,因为它需要读取堆栈跟踪,因此在大多数情况下尽量避免检查。
public class MyTaskScheduler : TaskScheduler
{
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{
if (this.CanInlineTasks() || // should return true most of the time
IsSynchronousContinuationTask(task))
return base.TryExecuteTaskInline(task, taskWasPreviouslyQueued);
return false;
}
private static bool IsSynchronousContinuationTask(Task task)
{
// assuming Mono
string stackTrace = Environment.StackTrace;
return stackTrace.Contains("System.Threading.Tasks.Task.RunSynchronouslyCore")
&& stackTrace.Contains("System.Threading.Tasks.TaskContinuation.Execute");
}
}