开发者

How to pass parameters to callback?

开发者 https://www.devze.com 2023-01-30 05:44 出处:网络
Code callback in C#: private void CallbackVisibleButton(IAsyncResult ar) { AsynchronousVisibleButtonDelegate asyncDeleg = (AsynchronousVisibleButtonDelegate)ar.AsyncState;

Code callback in C#:

private void CallbackVisibleButton(IAsyncResult ar)
{
    AsynchronousVisibleButtonDelegate asyncDeleg = (AsynchronousVisibleButtonDelegate)ar.AsyncState;
    b.Visibility = asyncDeleg.EndInvoke(ar);// b - not see!
}
private delegate Visibility AsynchronousVisibleButtonDelegate(Button b);
private Visibility AsynchronousVisibleButton(Button b)
{
    Thread.Sleep(2000);
    return Visibility.Visible;
}

and createted (b is Button, 5 seconds after the button should be visible):

AsynchronousVisibleButtonDelegate asyncDeleg = new AsynchronousVisibleButtonDelegate(Asynchron开发者_Go百科ousVisibleButton);
AsyncCallback callback = new AsyncCallback(CallbackVisibleButton);
asyncDeleg.BeginInvoke(b, callback, asyncDeleg);

problem: CallbackVisibleButton - does not see the button


Maybe the code below will solve your problem. Remove CallBackVisib... method and do as follows in your main code:

        AsynchronousVisibleButtonDelegate asyncDeleg = new AsynchronousVisibleButtonDelegate(AsynchronousVisibleButton);
        AsyncCallback callback = new AsyncCallback(p =>
                                                       {
                                                           var anotherState =
                                                               p.AsyncState as AsynchronousVisibleButtonDelegate;
                                                           b.Visible = anotherState.EndInvoke(p);
                                                       });
        asyncDeleg.BeginInvoke(b, callback, asyncDeleg);


use the third parameter of the BeginInvoke to send extra information. then you can get it through the IAsyncResult.AsyncState property.

here is an example: http://progtutorials.tripod.com/C_Sharp.htm

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号