This file contains hidden or 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.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace RStein.Async.Schedulers | |
{ | |
public class IoServiceThreadPoolScheduler : TaskSchedulerBase |
This file contains hidden or 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
Coroutine: 0 iteration 0 tid 1 | |
Coroutine: 1 iteration 0 tid 1 | |
Coroutine: 2 iteration 0 tid 1 | |
Coroutine: 0 before delay 0 tid 1 | |
Coroutine: 1 before delay 0 tid 1 | |
Coroutine: 2 before delay 0 tid 1 | |
Coroutine: 2 after delay 0 tid 1 | |
Coroutine: 2 before yield 0 tid 1 | |
Coroutine: 1 after delay 0 tid 1 | |
Coroutine: 1 before yield 0 tid 1 |
This file contains hidden or 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 static void testCoroutines() | |
{ | |
using (var tester = new LogCoroutineTester()) | |
{ | |
tester.Start(); | |
} | |
} |
This file contains hidden or 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.Globalization; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using RStein.Async.Schedulers; | |
namespace RStein.Async.Examples.Coroutines | |
{ | |
public class LogCoroutineTester : IDisposable | |
{ |
This file contains hidden or 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.Threading; | |
using System.Threading.Tasks; | |
namespace RStein.Async.Examples.Coroutines | |
{ | |
public class LogCoroutineMethod | |
{ | |
public const int DEFAULT_DELAY_MS = 500; | |
public const string ITERATION_MESSAGE_FORMAT = "Coroutine: {0,-20} iteration {1, -20} tid {2, -10}"; |
This file contains hidden or 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 RStein.Async.Schedulers; | |
namespace RStein.Async.Examples.Coroutines | |
{ | |
public class Coroutine : INotifyCompletion | |
{ | |
private readonly IoServiceScheduler m_ioServiceScheduler; |
This file contains hidden or 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
public async Task<string> WaitAsync() | |
{ | |
//Start metody, metoda běží synchronně | |
await Task.Delay(DEFAULT_DELAY_MS); | |
//”zapauzování metody” a odchod z metody | |
//Metoda je roztržena na dvě části – z řádku níže, který ještě neproběhl, je vytvořena tzv. “continuation” | |
// Continuation = delegát, který je předán awaiteru a ten je povinen delegáta spustit, až bude tásk vydaný z metody Delay dokončen. | |
//……………Metoda neběží |
This file contains hidden or 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
public virtual Action Wrap(Action action) | |
{ | |
checkIfDisposed(); | |
if (action == null) | |
{ | |
throw new ArgumentNullException("action"); | |
} | |
return () => Dispatch(action); | |
} |
This file contains hidden or 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
public virtual Task Post(Action action) | |
{ | |
checkIfDisposed(); | |
if (action == null) | |
{ | |
throw new ArgumentNullException("action"); | |
} | |
return postInner(() => Dispatch(action)); |
This file contains hidden or 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
public virtual Task Dispatch(Action action) | |
{ | |
checkIfDisposed(); | |
if (action == null) | |
{ | |
throw new ArgumentNullException("action"); | |
} | |
var task = Task.Factory.StartNew(action, | |
CancellationToken.None, |