Answer the question
In order to leave comments, you need to log in
Designing your own asynchronous functions, which pattern is correct?
public Task<int> Foo()
{
var tcs = new TaskCompletionSource<int>();
Task.Run(() => {
var result = SomeAction();
tcs.SetResult(result);
});
return tcs.Task;
}
Answer the question
In order to leave comments, you need to log in
I always use this:
//for a method that returns nothing
public async Task Blabla()
{
await Task.Run(()=>{
//тут я что-то выполняю
});
}
public async Task<string> Blabla()
{
return await Task.Run(()=>{
return "я возвращаюсь!!!";
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question