Last active
December 18, 2015 17:16
-
-
Save MykolaBalakin/1ae5dc7f3750dd25b5f5 to your computer and use it in GitHub Desktop.
await without TPL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async void AsyncMethod() { | |
var result = await Method(); | |
} | |
MyTask<Int32> Method() { | |
return new MyTask<Int32>(); | |
} | |
// ------------------------------------------------------------ | |
public class MyTask<T> { | |
public Boolean IsCompleted { get { throw new NotImplementedException(); } } | |
public Boolean Result { get { throw new NotImplementedException(); } } | |
public void ContinueWith(Action continuation) { | |
throw new NotImplementedException(); | |
} | |
} | |
public static class MyTaskExtensions { | |
public static MyAwaiter<T> GetAwaiter<T>(this MyTask<T> task) { | |
return new MyAwaiter<T>(task); | |
} | |
} | |
public class MyAwaiter<T> : INotifyCompletion { | |
private readonly MyTask<T> task; | |
public MyAwaiter(MyTask<T> task) { | |
this.task = task; | |
} | |
public Boolean IsCompleted | |
{ | |
get { return task.IsCompleted; } | |
} | |
public void OnCompleted(Action continuation) { | |
var context = SynchronizationContext.Current; | |
if (context == null) { | |
task.ContinueWith(continuation); | |
} else { | |
// with usage of context | |
task.ContinueWith(continuation); | |
} | |
} | |
public T GetResult() { | |
return task.Result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment