await Thread.SleepAsync()
追加のスレッドを作成せずに実行するのを待つことができるカスタムを実装しようとしています。
これが私が持っているものです:
class AwaitableThread : INotifyCompletion
{
public AwaitableThread(long milliseconds)
{
var timer = new Timer(obj => { IsCompleted = true; }, null, milliseconds, Timeout.Infinite);
}
private bool isCompleted = false;
public bool IsCompleted
{
get { return isCompleted; }
set { isCompleted = value; }
}
public void GetResult()
{}
public AwaitableThread GetAwaiter() { return this; }
public void OnCompleted(Action continuation)
{
if (continuation != null)
{
continuation();
}
}
}
そして、これが睡眠がどのように機能するかです:
static async Task Sleep(int milliseconds)
{
await new AwaitableThread(milliseconds);
}
OnCompleted
問題は、がまだfalseであっても、この関数がすぐに返されることIsCompleted
です。
私は何が間違っているのですか?