using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
public async static Task Main()
Console.WriteLine(System.Environment.ProcessorCount);
Console.WriteLine("Hello World");
var t=p.GetString("HeLLO ");
var tt=t.ContinueWith(r=>r.ToUpper());
Console.WriteLine("Daffy");
Console.WriteLine(await t);
Console.WriteLine(await tt);
Console.WriteLine("after call");
Console.WriteLine(CustomTask<string>.totalTask);
Console.WriteLine(CustomTask<string>.totalCompletedWithoutContinuation);
public async CustomTask<string> GetString(string s)
var t= new CustomTask<string>(()=>s+ "Bugs ");
Console.WriteLine($"In GetString: {t.Result}");
[AsyncMethodBuilder(typeof(CustomTaskBuilder<>))]
public class CustomTask<T>
internal static int totalTask=0;
internal static int totalCompletedWithoutContinuation=0;
private Func<T> function;
private CustomTaskAwaiter<T> awaiter;
private ValueTask<T> vTask;
private TaskCompletionSource<T> tcs=new TaskCompletionSource<T>();
TaskContinuationOptions tco=TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.ExecuteSynchronously;
internal CustomTask(T fromResult)
tcs.SetResult(fromResult);
public CustomTask(Func<T> f, bool continueOnCapturedContext=false)
TaskCreationOptions tCo=TaskCreationOptions.DenyChildAttach;
task=Task<T>.Factory.StartNew(()=>f(), tCo);
task.ConfigureAwait(continueOnCapturedContext);
private void initAwaiter(ref Task<T> t) => awaiter=new CustomTaskAwaiter<T>(ref t);
public CustomTaskAwaiter<T> GetAwaiter() => awaiter;
public T Result {get=>GetAwaiter().GetResult();}
public CustomTask<T> ContinueWith(Func<T,T> func) => (CustomTask<T>)task.ContinueWith<T>(antecedent=>func(antecedent.Result));
public CustomTask<OUT> ContinueWith<OUT>(Func<T, OUT> func) => (CustomTask<OUT>)task.ContinueWith<OUT>(antecedent=>func(antecedent.Result));
public static explicit operator CustomTask<T>(Task<T> task) => new CustomTask<T>(fromResult:task.Result);
public static explicit operator CustomTask<T>(T result) => new CustomTask<T>(fromResult:result);
public static explicit operator Task<T>(CustomTask<T> customTask) => customTask.task;
public struct CustomTaskAwaiter<T> : INotifyCompletion
private bool isCompleted=false;
public CustomTaskAwaiter(ref Task<T> t)
bool result=task.IsCompleted;
Interlocked.Increment(ref CustomTask<T>.totalTask);
if(result) Interlocked.Increment(ref CustomTask<T>.totalCompletedWithoutContinuation);
public T GetResult() => task.Result;
public void OnCompleted(Action continuation)
Console.WriteLine("Awaiter.OnCompleted");
if (continuation!=null) continuation();
public struct CustomTaskBuilder<T>
public CustomTaskBuilder() => Console.WriteLine(".ctor");
public static CustomTaskBuilder<T> Create() => new CustomTaskBuilder<T>();
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
Console.WriteLine("Start");
public CustomTask<T> Task {get;set;}=default(CustomTask<T>);
public void SetResult(T result)
Task=new CustomTask<T>(result);
Console.WriteLine($"{result} In CustomTaskBuilder.SetResult");
public void SetStateMachine(IAsyncStateMachine stateMachine)
this.SetStateMachine(stateMachine);
public void SetException(Exception exception)
ExceptionDispatchInfo.Throw(exception);
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine