App.xaml.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Windows;
  3. using System.Threading;
  4. using System.Reflection;
  5. using SHJX.Service.View.View;
  6. using SHJX.Service.Common.ReadXML;
  7. using SHJX.Service.Common.ExtendElement;
  8. using Microsoft.Extensions.Logging;
  9. using SHJX.Service.Common.Logging;
  10. using Microsoft.Extensions.Configuration;
  11. namespace SHJX.Service.View
  12. {
  13. /// <summary>
  14. /// App.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class App : Application {
  17. //private static readonly ILogger logger = LogFactory.BuildLogger(typeof(Rs485Client));
  18. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(App));
  19. private ReadConfigUtil config;
  20. protected override void OnStartup(StartupEventArgs e)
  21. {
  22. try
  23. {
  24. config = CreateConfig.CreateReadConfigUtil("shjx");
  25. var rAssembly = Assembly.GetEntryAssembly();
  26. var rProductAttribute = (AssemblyProductAttribute)rAssembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0];
  27. var mt = new Mutex(true, rProductAttribute.Product, out var ret);
  28. if (ret)
  29. {
  30. base.OnStartup(e);
  31. LoginForm login = new LoginForm(config);
  32. var result = login.ShowDialog();
  33. if (result is not null && Convert.ToBoolean(result))
  34. {
  35. MainForm main = new MainForm(config);
  36. main.ShowDialog();
  37. }
  38. }
  39. else
  40. {
  41. UMessageBox.Error("有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。");
  42. Current.Shutdown();//退出程序
  43. return;
  44. }
  45. mt.ReleaseMutex();
  46. Environment.Exit(0);
  47. }
  48. catch (Exception ex)
  49. {
  50. //logger.LogError(ex.ToString());
  51. logger?.LogError(ex.ToString());
  52. //logger.LogError(ex.ToString());
  53. }
  54. }
  55. }
  56. }