-
-
Save fengli320/d8a45aab6398c9f7fbc9b56bcdc7c453 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Runtime.CompilerServices; | |
using System.Threading.Tasks; | |
namespace StateMachineConceptual | |
{ | |
class Program | |
{ | |
static async Task Main() | |
{ | |
var res = await MyAsyncMethod(500, 1000); | |
Console.WriteLine(res); | |
} | |
private static Task<int> MyAsyncMethod(int firstDelay, int secondDelay) | |
{ | |
var stateMachine = new MyAsyncMethodStateMachine(firstDelay, secondDelay, 0); | |
stateMachine.MoveNext(); | |
return stateMachine.ResultTask; | |
} | |
private class MyAsyncMethodStateMachine | |
{ | |
private readonly TaskCompletionSource<int> _taskBuilder = new TaskCompletionSource<int>(); | |
private readonly int _firstDelay; | |
private readonly int _secondDelay; | |
private int _state; | |
private TaskAwaiter _awaiter; | |
public MyAsyncMethodStateMachine(int firstDelay, int secondDelay, int initialState) | |
{ | |
_firstDelay = firstDelay; | |
_secondDelay = secondDelay; | |
_state = initialState; | |
} | |
public void MoveNext() | |
{ | |
try | |
{ | |
if (_state == 0) | |
{ | |
Console.WriteLine("Before first await."); | |
_state = 1; | |
_awaiter = Task.Delay(_firstDelay).GetAwaiter(); | |
if (!_awaiter.IsCompleted) | |
{ | |
_awaiter.OnCompleted(MoveNext); | |
return; | |
} | |
} | |
if (_state == 1) | |
{ | |
_awaiter.GetResult(); | |
Console.WriteLine("Before second await."); | |
_state = 2; | |
_awaiter = Task.Delay(_secondDelay).GetAwaiter(); | |
if (!_awaiter.IsCompleted) | |
{ | |
_awaiter.OnCompleted(MoveNext); | |
return; | |
} | |
} | |
Console.WriteLine("Done."); | |
_taskBuilder.SetResult(42); | |
} | |
catch (Exception ex) | |
{ | |
_taskBuilder.SetException(ex); | |
} | |
} | |
public Task<int> ResultTask => _taskBuilder.Task; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment