ErrorHandlers.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Windows;
  5. namespace SAGA.RevitUtils.ErrorSupports
  6. {
  7. public class ErrorHandlers
  8. {
  9. private static AppDomain _appDoamin;
  10. private static void appDoamin_DomainUnload(object sender, EventArgs e)
  11. {
  12. _appDoamin = null;
  13. }
  14. private static void appDoamin_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  15. {
  16. if (!TErrorSupport.IsShowed)
  17. {
  18. MessageShow.Show(e.ExceptionObject as Exception, false, "");
  19. }
  20. }
  21. public static void Start()
  22. {
  23. if (_appDoamin == null)
  24. {
  25. _appDoamin = AppDomain.CurrentDomain;
  26. _appDoamin.UnhandledException += new UnhandledExceptionEventHandler(ErrorHandlers.appDoamin_UnhandledException);
  27. _appDoamin.DomainUnload += new EventHandler(ErrorHandlers.appDoamin_DomainUnload);
  28. }
  29. //界面异常捕获 xls 2018-8-7
  30. // Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
  31. //var tempPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(ErrorHandlers)).Location);
  32. //var path = Environment.GetEnvironmentVariable("PATH") + tempPath;
  33. //Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);
  34. //string assemblyName = "System.Net.Http.Formatting, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
  35. //Assembly.Load(assemblyName);
  36. }
  37. private static void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  38. {
  39. if (!TErrorSupport.IsShowed)
  40. {
  41. MessageShow.Show(e.Exception as Exception, false, "");
  42. }
  43. e.Handled = true;
  44. }
  45. public static void Stop()
  46. {
  47. if (_appDoamin != null)
  48. {
  49. _appDoamin.UnhandledException -= new UnhandledExceptionEventHandler(ErrorHandlers.appDoamin_UnhandledException);
  50. _appDoamin.DomainUnload -= new EventHandler(ErrorHandlers.appDoamin_DomainUnload);
  51. _appDoamin = null;
  52. }
  53. //Application.Current.DispatcherUnhandledException -= Current_DispatcherUnhandledException;
  54. }
  55. }
  56. }