Skip to content

Instantly share code, notes, and snippets.

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
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
private static void testCoroutines()
{
using (var tester = new LogCoroutineTester())
{
tester.Start();
}
}
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
{
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}";
using System;
using System.Runtime.CompilerServices;
using RStein.Async.Schedulers;
namespace RStein.Async.Examples.Coroutines
{
public class Coroutine : INotifyCompletion
{
private readonly IoServiceScheduler m_ioServiceScheduler;
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ěží
public virtual Action Wrap(Action action)
{
checkIfDisposed();
if (action == null)
{
throw new ArgumentNullException("action");
}
return () => Dispatch(action);
}
public virtual Task Post(Action action)
{
checkIfDisposed();
if (action == null)
{
throw new ArgumentNullException("action");
}
return postInner(() => Dispatch(action));
public virtual Task Dispatch(Action action)
{
checkIfDisposed();
if (action == null)
{
throw new ArgumentNullException("action");
}
var task = Task.Factory.StartNew(action,
CancellationToken.None,