I wanted a way to call a method asynchronously and have a function be fired upon completion. This is partially inspired by AJAX calls in web applications. Is my implementation ok? Is there a better way to do this?
public static class Tools
{
public static void RunAsync(Action function, Action callback)
{
BackgroundWorker worker = new BackgroundWorker();
worker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e)
{
callback();
};
worker.DoWork += delegate(object sender, DoWorkEventArgs e)
{
function();
};
worker.RunWorkerAsync();
}
}
Is there a good way to extend this to work with functions that take arguments and have return values?
Thanks!