Forked from hjerpbakk/ViewModelWithBackgroundWorker.cs
Last active
December 10, 2015 10:27
-
-
Save jrgcubano/78a818ba3d2a6387218a 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 ViewModelWithBackgroundWorker { | |
private readonly BackgroundWorker worker; | |
public ViewModelWithBackgroundWorker() { | |
worker = new BackgroundWorker(); | |
worker.DoWork += DoSomething; | |
worker.RunWorkerCompleted += WorkCompleted; | |
} | |
public string Message { get; set; } | |
public string Result { get; set; } | |
public void ExecuteAsync() { | |
Message = "Loading..."; | |
worker.RunWorkerAsync(); | |
} | |
private void DoSomething(object sender, DoWorkEventArgs e) { | |
e.Result = "Result"; | |
} | |
private void WorkCompleted(object sender, RunWorkerCompletedEventArgs e) { | |
if (e.Error != null) { | |
HandleError(e.Error); | |
} else { | |
Message = "Completed"; | |
Result = (string)e.Result; | |
} | |
} | |
private void HandleError(Exception exception) { | |
Message = exception.Message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment