fixed the issue. Will look into that further 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());
}
}
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());
}
}
I hope this helps you . No can do. When someone expects a Func f you can assume it will be invoked with something like result = f() - i.e., it does not know about async behavior. If you cheat it by using .Result like you have - it will deadlock on UI thread because it wants to schedule the code after await (in _Fetch) on the UI thread, but you have already blocked it with .Result. Async lambda can be passed to Action since it has no return value - or to Func or Func>.
Cannot convert async lambda expression to delegate type 'Func<bool, string, bool>'
seems to work fine I would personally use the foreach implementation, but answering the concrete question. Without async the Aggregate overload used has the following signature:
it should still fix some issue Okay, so I've got I got a VB project that I'm converting to C#. So far, so good. The problem is delegates/actions are quite different between the two languages and I'm struggling to work out the difference. , Your register method should be defined as:
hop of those help? Your whole method is suboptimal. You could rewrite your code to be much simpler. A few comments on your existing code first, however. You're using Task.Factory.StartNew(), which is dangerous. In most cases you should simply use Task.Run() You're using Task.Wait() and Task.Result, which is also suboptimal, not to mention, that Task.Result includes Task.Wait() when accessing it. But I guess, that you want to test the lambda, so it's ok here.
private int Server_Get_Int(){
var task = Task.Run(async () => {
var c = Server_Connect();
return (await c.GetAsync("todos/set")).ResultAs<int>();
});
int result = task.Result;
Console.WriteLine(result);
return result;
}
private async Task<int> Server_Get_Int_Async(){
return await Task.Run(async () => {
var c = Server_Connect();
return (await c.GetAsync("todos/set")).ResultAs<int>();
});
}
public async Task Foo()
{
// This is the fetch task that's going to be completed sometime in the future. You should almost in any case use configure await on your tasks. For reasons see below.
Task<int> intTask = Server_Get_Int_Async().ConfigureAwait(false);
// Do something other with the task object
// Finally await it and print
int result = await intTask;
Console.WriteLine(result);
}
// Do this if you just need the result and nothing else.
public async Task Bar()
{
int result = await Server_Get_Int_Async().ConfigureAwait(false);
Console.WriteLine(result);
}