12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using Vinno.FIS.Sonopost.Common;
- namespace Vinno.FIS.Sonopost.Keeper
- {
- /// <summary>
- /// App.xaml 的交互逻辑
- /// </summary>
- public partial class App : Application
- {
- private string _sonopostPath;
- private ProcessKeepHelper _keepHelper;
- protected override void OnStartup(StartupEventArgs args)
- {
- try
- {
- base.OnStartup(args);
- CheckedOpenRepeatedly();
- Thread.Sleep(2000);
- try
- {
- ProcessHelper.FinishProcessByName("Vinno.FIS.Sonopost.Upgrade");
- ProcessHelper.FinishProcessByName("Sonopost");
- ProcessHelper.FinishProcessByName("Vinno.FIS.Sonopost.TRTCClient");
- }
- catch (Exception e)
- {
- WriteLog(e.ToString());
- }
- _sonopostPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sonopost.exe");
- var processId = ProcessHelper.StartProcess(_sonopostPath);
- _keepHelper = new ProcessKeepHelper(processId);
- _keepHelper.StartKeepProcess(() =>
- {
- return ProcessHelper.StartProcess(_sonopostPath);
- });
- }
- catch (Exception e)
- {
- WriteLog(e.ToString());
- }
- }
- protected override void OnExit(ExitEventArgs e)
- {
- _keepHelper?.StopKeepProcess();
- base.OnExit(e);
- }
- private void WriteLog(string msg)
- {
- var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SonopostKeeperLog.log");
- File.AppendAllText(path, msg);
- }
- /// <summary>
- /// 判断是否重复打开
- /// </summary>
- private void CheckedOpenRepeatedly()
- {
- var thisProcess = Process.GetCurrentProcess();
- if (Process.GetProcessesByName(thisProcess.ProcessName).Where(x => x.ProcessName == thisProcess.ProcessName).Count() > 1)
- {
- thisProcess.Kill();
- }
- }
- }
- }
|