I would like to write the following:
public string GetSomeValue()
{
//directly return the value of the Method 'DoSomeHeavyWork'...
var t = DoSomeHeavyWork();
return t.Result;
}
public Task<string> DoSomeHeavyWork()
{
return Task.Run(() => {
// do some long working progress and return a string
return "Hello World!";
});
}
As you can see to return the result from the DoSomeHeavyWork() I have used the Task.Result property, which works okay, but according to researches this will block the Thread.
I would like to use the async/await pattern for this but cant seem to find how to do this. If I did the same with async/await with my current knowledge I will always end up with this:
public async Task<string> GetSomeValue()
{
//directly return the value of the Method 'DoSomeHeavyWork'...
var t = DoSomeHeavyWork();
return await t;
}
public Task<string> DoSomeHeavyWork()
{
return Task.Run(() => {
// do some long working progress and return a string
return "Hello World!";
});
}
This solution doesnt quite fit my needs because I want to return ONLY the string and not a Task<string>, how can this be achieved with async/await?
Task<string>because when youawaitit it returns astring. Isn't that what you want?