【问题标题】:CS4010 How to convert async lambda expression to delegate type 'TaskAction'CS4010 如何将异步 lambda 表达式转换为委托类型“TaskAction”
【发布时间】:2019-12-08 15:31:16
【问题描述】:

我收到以下错误

错误 CS4010 无法将异步 lambda 表达式转换为委托类型 “任务行动”。异步 lambda 表达式可能返回 void、Task 或 Task,它们都不能转换为 'TaskAction'

我的函数如下所示:

public async void GetLastKnownUpdateStatus(string UUIDStr, short ClientId)
{
    UpdateStatusInfo x = new UpdateStatusInfo();
    if (Execution.WaitForTask(async (canceltoken) => { x = await GetLastKnownUpdateStatus(UUIDStr, ClientId); return true; }) > Execution.WAIT_TIMEOUT)
    {
        Output.WriteLine("GetLastKnownUpdateStatus: "+ x.State.ToString());
    }
}

此方法调用以下函数:

public Task<UpdateStatusInfo> GetLastKnownUpdateStatus(string uniqueUpdateID, short clientID)
{
    return GetLastKnownUpdateStatus(uniqueUpdateID, clientID, null);
}

感谢任何帮助

Execution.WaitForTask 来自 Vector.CANoe.Threading 类,由 Vector 定义如下

//
// Summary:
//     Executes a task in a separate thread. During the wait, the measurement and simulation
//     are not blocked. Optionally returns failure after a certain timespan.
//
// Parameters:
//   taskAction:
//     A delegate function to execute in a separate task
//
//   maxTime:
//     Optional: maximum time to wait, in milliseconds.
//
// Returns:
//     WAIT_TIMEOUT: if an maxTime was defined and the task did not return within maxTime
//     milliseconds WAIT_ABORTED: if the measurement was stopped during task execution
//     WAIT_EXCEPTION: if an exception occurred in the taskAction delegate WAIT_ILLEGAL_RESULTVALUE:
//     the result provided by the task is <= 0 > 0 any positive result provided by the
//     taskAction delegate (only use numbers > 0 as return values)
//
// Remarks:
//     Be careful: You may not use most of the CANoe API functions in the taskAction.
//     Allowed is: Modifying SystemVariables Using Output.* functions See documentation
//     for details

【问题讨论】:

  • 如果您重新格式化代码以避免排长队并提供minimal reproducible example,这将非常有帮助。目前我们不知道TaskAction 是什么,并且在您提供的代码中从未提及。我们也不知道Execution.WaitForTask 是什么。如果没有所有相关信息,我们将无法提供帮助。

标签: c# asynchronous lambda delegates canoe


【解决方案1】:

TaskAction 可能不是异步表达式。它必须是同步的。 CANoe 的框架将确保它在后台任务中执行。

最好是调用 GetLastKnownUpdateStatus 最终直接调用的任何同步方法

public async void GetLastKnownUpdateStatus(string UUIDStr, short ClientId)
{
    UpdateStatusInfo x = new UpdateStatusInfo();
    if (Execution.WaitForTask((canceltoken) => {
          x = GetLastKnownUpdateStatusSync(UUIDStr, ClientId); return true;
        }
    ) > Execution.WAIT_TIMEOUT)
    {
        Output.WriteLine("GetLastKnownUpdateStatus: "+ x.State.ToString());
    }
}

或等待 lambda 中的调用:

public async void GetLastKnownUpdateStatus(string UUIDStr, short ClientId)
{
    UpdateStatusInfo x = new UpdateStatusInfo();
    if (Execution.WaitForTask((canceltoken) => { 
          x = GetLastKnownUpdateStatus(UUIDStr, ClientId).ConfigureAwait(false).GetAwaiter().GetResult(); return true;
        }
    ) > Execution.WAIT_TIMEOUT)
    {
        Output.WriteLine("GetLastKnownUpdateStatus: "+ x.State.ToString());
    }
}

【讨论】:

  • 谢谢,第一个解决方案行不通,因为您无法将类型任务转换为无效。但是您的第二个解决方案对我有用.ConfigureAwait(false).GetAwaiter().GetResult() 真的对这些东西感到困惑,不知道为什么它会起作用
猜你喜欢
  • 2013-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
相关资源
最近更新 更多