开发者

How to pass parameters to callback?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜