如何将任务的状态设置为“正在运行"?通过任务完成源 [英] How can I set the state of a task to "Running" via TaskCompletionSource

查看:24
本文介绍了如何将任务的状态设置为“正在运行"?通过任务完成源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中的某些情况下,我使用 TaskCompletionSource 来创建任务并操纵其状态.
在其他情况下,任务以常规"方式创建和执行.方式.

In some cases in my program, I use a TaskCompletionSource to create a task and manipulate its state.
In other cases, the task is created and executed in a "regular" way.

在常规任务创建和执行的情况下,任务的状态为正在运行";在执行过程中.这个跑"在其他地方检查状态以确保任务正在执行?

In case of regular task creation and execution, the state of the task is "Running" while it's being executed. This "Running" state is checked at other places to ensure the task is being executed?

如何在使用 TaskCompletionSource 时在任务上设置此状态?如果我不将状态设置为正在运行",则上述检查将失败.

How can I set this state on the task when using the TaskCompletionSource? If I don't set the state to "Running", then the mentioned check fails.

推荐答案

我发现实现这一目标的唯一方法是通过反射.以下解决方案已经过测试并且可以正常工作.

The only way I have found to achieve that is via reflection. The following solution is tested and it's working.

private const string s_fieldName_StateFlags = "m_stateFlags";

/// <summary> From https://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/Task.cs </summary>
[Flags]
public enum TaskState
{
  TASK_STATE_STARTED                      = 0x10000,    //bin: 0000 0000 0000 0001 0000 0000 0000 0000
  TASK_STATE_DELEGATE_INVOKED             = 0x20000,    //bin: 0000 0000 0000 0010 0000 0000 0000 0000
  TASK_STATE_DISPOSED                     = 0x40000,    //bin: 0000 0000 0000 0100 0000 0000 0000 0000
  TASK_STATE_EXCEPTIONOBSERVEDBYPARENT    = 0x80000,    //bin: 0000 0000 0000 1000 0000 0000 0000 0000
  TASK_STATE_CANCELLATIONACKNOWLEDGED     = 0x100000,   //bin: 0000 0000 0001 0000 0000 0000 0000 0000
  TASK_STATE_FAULTED                      = 0x200000,   //bin: 0000 0000 0010 0000 0000 0000 0000 0000
  TASK_STATE_CANCELED                     = 0x400000,   //bin: 0000 0000 0100 0000 0000 0000 0000 0000
  TASK_STATE_WAITING_ON_CHILDREN          = 0x800000,   //bin: 0000 0000 1000 0000 0000 0000 0000 0000
  TASK_STATE_RAN_TO_COMPLETION            = 0x1000000,  //bin: 0000 0001 0000 0000 0000 0000 0000 0000
  TASK_STATE_WAITINGFORACTIVATION         = 0x2000000,  //bin: 0000 0010 0000 0000 0000 0000 0000 0000
  TASK_STATE_COMPLETION_RESERVED          = 0x4000000,  //bin: 0000 0100 0000 0000 0000 0000 0000 0000
  TASK_STATE_THREAD_WAS_ABORTED           = 0x8000000,  //bin: 0000 1000 0000 0000 0000 0000 0000 0000
  TASK_STATE_WAIT_COMPLETION_NOTIFICATION = 0x10000000, //bin: 0001 0000 0000 0000 0000 0000 0000 0000
  TASK_STATE_EXECUTIONCONTEXT_IS_NULL     = 0x20000000, //bin: 0010 0000 0000 0000 0000 0000 0000 0000
  TASK_STATE_TASKSCHEDULED_WAS_FIRED      = 0x40000000, //bin: 0100 0000 0000 0000 0000 0000 0000 0000
}

public  static void SetTaskRunning<TResult> (TaskCompletionSource<TResult> i_taskCompletionSource)
{
  if (i_taskCompletionSource == null)
    throw new ArgumentNullException (nameof (i_taskCompletionSource));

  var value = TaskState.TASK_STATE_STARTED
           |  TaskState.TASK_STATE_DELEGATE_INVOKED;

  var task      = i_taskCompletionSource.Task;
  var type      = task.GetType ();
  var fieldInfo = type.GetField (s_fieldName_StateFlags, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  fieldInfo.SetValue (task, (int)value);
}

这篇关于如何将任务的状态设置为“正在运行"?通过任务完成源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆