我想获得一个异步委托。我创建了一个简单的代码来了解我的问题在哪里。我一直在阅读 async/await 文档,但所有情况都很简单。
我的代码应用异步委托:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
int code;
CustomDialog dialog = new CustomDialog (this);
dialog.Select ();
dialog.Finish += (t) =>
{
code = t.code;
};
//
//Wait until app executes dialog.finish().
//I don't want put the UIAlertView in "dialog.Finish".
//
UIAlertView alert = new UIAlertView ("dialog later", "item select " + code, null, null, "ok");
alert.Show ();
}
public class CustomType
{
public int code { get; set; }
}
public class CustomDialog
{
public event DialogoEventHandle Finish;
public delegate void DialogoEventHandle (CustomType t);
private UIViewController view;
public CustomDialog(UIViewController view)
{
this.view = view;
}
protected void OnFinish(CustomType t)
{
if (Finish != null)
Finish(t);
}
public void Select()
{
ThreadPool.QueueUserWorkItem ((object sender) =>
{
//operation
Thread.Sleep (400);
this.view.InvokeOnMainThread (() =>
{
OnFinish (new CustomType () { code = 5 });
});
});
}
}
}