2016-09-01 7 views
1

Ich habe eine Anwendung, die ferngesteuert wird. Das Programm muss Animation auf Anfrage und Antwort auf den Client spielen, wenn es fertig ist. Dies ist der Code, den ich jetzt habe (und es funktioniert nicht richtig):Nancy Route - Rückgabewert beim Rückruf

public void PlayAnimation(Action callback) 
{ 
    DoubleAnimation fadeOut = new DoubleAnimation 
    { 
     //settings 
    }; 
    fadeOut.Completed += (s, e) => callback(); 
    BeginAnimation(OpacityProperty, fadeOut); 
} 


Get["/playAnim/{id}"] = param => 
{ 
    MainWindow.PlayAnimation(() => {/* Need to call "return" statement here */}); 
    return "Ok"; // This is where the value is returned now. 
    //The execution gets here before animation was completed. 
}; 

Ich weiß, Nancy unterstützt auch async-await Syntax, aber DoubleAnimation nicht (oder es tut?). Also, wie mache ich Nancy antworten nach die Animation wurde gespielt?

Antwort

1

Wie Sie selbst gesagt haben - Nancy unterstützt asynchrone Methoden, so dass Sie hier TaskCompletionSource verwenden können. Der zweite Teil Ihres Codes sieht wie folgt aus:

Get["/playAnim/{id}"] = async param => 
{ 
    var completionSource = new TaskCompletionSource<bool>(); 
    MainWindow.PlayAnimation(() => { completionSource.SetResult(true); }); 

    await completionSource.Task; 

    return "Ok"; 
};