Om Choudhary
Om Choudhary

Reputation: 512

CS4010 How to convert async lambda expression to delegate type 'TaskAction'

I am getting the following error

Error CS4010 Cannot convert async lambda expression to delegate type 'TaskAction'. An async lambda expression may return void, Task or Task, none of which are convertible to 'TaskAction'

My function looks like this:

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());
    }
}

This method calls the following function:

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

any help is appreciated

Execution.WaitForTask comes from Vector.CANoe.Threading class and is defined by Vector as following

//
// 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

Upvotes: 0

Views: 3596

Answers (1)

mspiller
mspiller

Reputation: 3839

The TaskAction may not be an async expression. It has to be synchronous. CANoe's framework will make sure that it is executed in a background task.

Best would be to call whatever synchronous method GetLastKnownUpdateStatus is finally calling to directly like this

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());
    }
}

or await the call in the 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());
    }
}

Upvotes: 2

Related Questions