Created
October 1, 2013 19:37
-
-
Save hjerpbakk/6783905 to your computer and use it in GitHub Desktop.
From: http://hjerpbakk.com/blog/2013/10/1/async-method-caller-easy-async-without-await.html
View the complete code in: https://github.com/Sankra/CSharpExamples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ViewModelWithAsyncMethodCaller { | |
private readonly AsyncMethodCaller asyncMethodCaller; | |
public ViewModelWithAsyncMethodCaller(AsyncMethodCaller asyncMethodCaller) { | |
this.asyncMethodCaller = asyncMethodCaller ?? new AsyncMethodCaller(); | |
} | |
public string Message { get; set; } | |
public string Result { get; set; } | |
public void ExecuteAsync() { | |
Message = "Loading..."; | |
asyncMethodCaller.CallMethodAndContinue(DoSomething, WorkCompleted, HandleError); | |
} | |
private string DoSomething() { | |
return "Result"; | |
} | |
private void WorkCompleted(string result) { | |
Message = "Completed"; | |
Result = result; | |
} | |
private void HandleError(Exception exception) { | |
Message = exception.Message; | |
} | |
} | |
[TestFixture] | |
public class ViewModelTests { | |
[Test] | |
public void DoSomething() { | |
var asyncMethodCaller = new TestAsyncMethodCaller(); | |
var viewModel = new ViewModelWithAsyncMethodCaller(asyncMethodCaller); | |
viewModel.ExecuteAsync(); | |
Assert.AreEqual("Loading...", viewModel.Message); | |
asyncMethodCaller.StartServiceAndWait(); | |
Assert.AreEqual("Result", viewModel.Result); | |
Assert.AreEqual("Completed", viewModel.Message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment