The following reduced example generates this compiler warning:
warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
public class TaskInterlockedExchangeTest
{
private Task _Task;
public async Task DoSomething()
{
Interlocked.Exchange(ref _Task, null);
await _Task;
}
}
The warning is raised on the line with the Interlocked.Exchange
call. As far as I'm aware Interlocked.Exchange
is not async
so why on earth is the compiler triggering this warning? Right now this doesn't make any sense to me, so what am I missing?
We do treat warnings as errors so I'm trying to figure out how to fix this (short of disabling the warning around the offending code which I would only see as last resort).
This happens on VS2013 (update 5).
Update
I just discovered that the following avoids the warning:
public class TaskInterlockedExchangeTest
{
private Task _Task;
public async Task DoSomething()
{
var t = Interlocked.Exchange(ref _Task, null);
await _Task;
}
}
So simply assigning the result to a local variable is sufficient.
async
method maybe the same warning will appear. – csharpfolkInterlocked
, which is aTask
. Stop ignoring it, and the warning will go away :) – Luaannull
... You probably meantvar task = Interlocked.Exchange(ref _Task, null); if (task != null) await task;
– Lucas Trzesniewski