Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void AsyncHelp<T>(Func<T> f, Action<Exception> econt, Action<T> cont) {
- var t = new Thread((_) => {
- try {
- var res = f();
- System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => cont(res));
- } catch (Exception ex) {
- System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => econt(ex));
- }
- });
- t.Start();
- }
- some_handler() {
- var id = mytextbox.Text; // Save from UI to local
- AsyncHelp(
- () => GetBla(id),
- bla => result.Text = bla.ToString(), // This is safe cause it's dispatched
- ex => error.Text = ex.ToString()
- );
- }
- Exception ex;
- Result r;
- var mre = new ManualResetEvent(false);
- myService.OnGetBlaCompleted += (_, e) => {
- ex = e.Error;
- r = e.Result;
- mre.Set();
- }
- myService.GetBlaAsync(id);
- mre.WaitOne();
- if (ex != null) throw ex;
- // and so on
Add Comment
Please, Sign In to add comment