2010-12-14 2 views
1

code de rappel en C#:Comment passer des paramètres à un rappel?

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; 
} 

et createted (b est Bouton, 5 secondes après que le bouton doit être visible):

AsynchronousVisibleButtonDelegate asyncDeleg = new AsynchronousVisibleButtonDelegate(AsynchronousVisibleButton); 
AsyncCallback callback = new AsyncCallback(CallbackVisibleButton); 
asyncDeleg.BeginInvoke(b, callback, asyncDeleg); 

problème: CallbackVisibleButton - ne voit pas le bouton

Répondre

1

le code ci-dessous peut-être résoudre votre problème. Supprimez la méthode CallBackVisib ... et procédez comme suit dans votre code principal:

 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); 
1

utilisez le troisième paramètre du BeginInvoke pour envoyer des informations supplémentaires. alors vous pouvez l'obtenir à travers la propriété IAsyncResult.AsyncState.

est ici un exemple: http://progtutorials.tripod.com/C_Sharp.htm

Questions connexes