code_junkie

How to get information about an exception raised by the target of Control.Invoke

Nov 12th, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. public static void Invoke(Control ctrl, Action action)
  2. {
  3. if (ctrl == null)
  4. throw new ArgumentNullException("ctrl");
  5. if (action == null)
  6. return;
  7.  
  8. var invokeRequired = ctrl.InvokeRequired;
  9. try
  10. {
  11. if (ctrl.InvokeRequired)
  12. ctrl.Invoke(action);
  13. else
  14. action();
  15. }
  16. catch (Exception ex)
  17. {
  18. throw new Exception(String.Format("Invoke error, ctrl={0}, action Target={1}", ctrl.Name, action.Target), ex);
  19. }
  20. }
  21.  
  22. public static void Invoke(Control ctrl, Action action, string context)
  23. {
  24. if (ctrl == null)
  25. throw new ArgumentNullException("ctrl");
  26. if (action == null)
  27. return; //not sure it's worththrowing an exception here...
  28.  
  29. var invokeRequired = ctrl.InvokeRequired;
  30. try
  31. {
  32. if (invokeRequired)
  33. ctrl.Invoke(action);
  34. else
  35. action();
  36. }
  37. catch (Exception ex)
  38. {
  39. var ps = invokeRequired ? "" : " - has the target control been initialised?";
  40. var errmsg = String.Format("Invoke error, ctrl={0}, action Target={1}, context={2}{3}", ctrl.Name, action.Target, context, ps);
  41. throw new Exception(errmsg, ex);
  42. }
  43. }
Add Comment
Please, Sign In to add comment