public static void DoWork(this UIViewController controller, Action work, Action<Exception> error = null, Action final = null)
{
MBProgressHUD hud = null;
hud = new MBProgressHUD(controller.View.Superview) {Mode = MBProgressHUDMode.Indeterminate, TitleText = "Loading..."};
controller.View.Superview.AddSubview(hud);
hud.Show(true);
ThreadPool.QueueUserWorkItem(delegate {
try
{
Utilities.PushNetworkActive();
work();
}
catch (Exception e)
{
if (error != null)
error(e);
controller.InvokeOnMainThread(delegate {
Utilities.ShowAlert("Error", e.Message);
});
}
finally
{
Utilities.PopNetworkActive();
if (final != null)
controller.InvokeOnMainThread(() => final());
}
if (hud != null)
{
controller.InvokeOnMainThread(delegate {
hud.Hide(true);
hud.RemoveFromSuperview();
});
}
});
}