What is the difference between these two methods, they compile differently but you can call both of them in the same way with the await keyword. I am wanting to use these for defining my WCF interfaces with an await keyword on Entity Framework 6.1.
Just interested which of these two methods is more correct for the use over WCF, im using a service factory host (no add service reference) so do not get the generated async methods.
public static Task<string> MethodA()
{
Console.WriteLine("{0} - Running MethodA on thread {1}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId);
var task = Task.Run(() =>
{
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("{0} - Done MethodA on thread {1}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId);
return string.Format("You ran MethodA on thread {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
});
Console.WriteLine("{0} - MethodA Returning on thread {1}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId);
return task;
}
public static async Task<string> MethodB()
{
Console.WriteLine("{0} - Running MethodB on thread {1}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId);
var task = Task.Run(() =>
{
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine("{0} - Done MethodB on thread {1}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId);
return string.Format("You ran MethodB on thread {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
});
Console.WriteLine("{0} - MethodB Returning on thread {1}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId);
return await task;
}
To provide some more info on this, after reading all the responses you are correct. the difference is in the state generation so I guess if we are not going to be using the state the first method is more correct.
below is the de-compiled code
public static Task<string> MethodA()
{
Console.WriteLine("{0} - Running MethodA on thread {1}", (object) DateTime.Now, (object) Thread.CurrentThread.ManagedThreadId);
if (Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegatea == null)
{
// ISSUE: method pointer
Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegatea = new Func<string>((object) null, __methodptr(\u003CMethodA\u003Eb__9));
}
Task<string> task = Task.Run<string>(Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegatea);
Console.WriteLine("{0} - MethodA Returning on thread {1}", (object) DateTime.Now, (object) Thread.CurrentThread.ManagedThreadId);
return task;
}
public static Task<string> MethodB()
{
Program.\u003CMethodB\u003Ed__d stateMachine;
stateMachine.\u003C\u003Et__builder = AsyncTaskMethodBuilder<string>.Create();
stateMachine.\u003C\u003E1__state = -1;
stateMachine.\u003C\u003Et__builder.Start<Program.\u003CMethodB\u003Ed__d>(ref stateMachine);
return stateMachine.\u003C\u003Et__builder.Task;
}
[CompilerGenerated]
private static void \u003CMain\u003Eb__0()
{
Program.CallServices();
}
[CompilerGenerated]
private static string \u003CMethodA\u003Eb__9()
{
Thread.Sleep(TimeSpan.FromSeconds(2.0));
Console.WriteLine("{0} - Done MethodA on thread {1}", (object) DateTime.Now, (object) Thread.CurrentThread.ManagedThreadId);
return string.Format("You ran MethodA on thread {0}", (object) Thread.CurrentThread.ManagedThreadId);
}
[CompilerGenerated]
private static string \u003CMethodB\u003Eb__b()
{
Thread.Sleep(TimeSpan.FromSeconds(2.0));
Console.WriteLine("{0} - Done MethodB on thread {1}", (object) DateTime.Now, (object) Thread.CurrentThread.ManagedThreadId);
return string.Format("You ran MethodB on thread {0}", (object) Thread.CurrentThread.ManagedThreadId);
}