private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { _controller?.Start(); } catch (Exception ex) { Logging.LogUsefulException(ex); } finally { try { System.Timers.Timer timer = (System.Timers.Timer)sender; timer.Enabled = false; timer.Stop(); timer.Dispose(); } catch (Exception ex) { Logging.LogUsefulException(ex); } } }
static void Main() { // Check OS since we are using dual-mode socket if (!Utils.IsWinVistaOrHigher()) { MessageBox.Show(I18N.GetString("Unsupported operating system, use Windows Vista at least."), "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Utils.ReleaseMemory(true); using (Mutex mutex = new Mutex(false, "Global\\Shadowsocks_" + Application.StartupPath.GetHashCode())) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.ApplicationExit += Application_ApplicationExit; SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ApplicationExit += (sender, args) => HotKeys.Destroy(); if (!mutex.WaitOne(0, false)) { Process[] oldProcesses = Process.GetProcessesByName("Shadowsocks"); if (oldProcesses.Length > 0) { Process oldProcess = oldProcesses[0]; } MessageBox.Show(I18N.GetString("Find Shadowsocks icon in your notify tray.") + "\n" + I18N.GetString("If you want to start multiple Shadowsocks, make a copy in another directory."), I18N.GetString("Shadowsocks is already running.")); return; } /** * 当前用户是管理员的时候,直接启动应用程序 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 */ //获得当前登录的Windows用户标示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //判断当前登录用户是否为管理员 if (!principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { var process = System.Diagnostics.Process.GetCurrentProcess(); var filename = process.MainModule.FileName; //创建启动对象 var p = new System.Diagnostics.Process(); p.StartInfo.UseShellExecute = true; p.StartInfo.WorkingDirectory = new FileInfo(filename).DirectoryName; p.StartInfo.FileName = filename; //设置启动动作,确保以管理员身份运行 p.StartInfo.Verb = "runas"; try { p.Start(); } catch { } //退出 Environment.Exit(0); } Directory.SetCurrentDirectory(Application.StartupPath); #if DEBUG Logging.OpenLogFile(); // truncate privoxy log file while debugging string privoxyLogFilename = Utils.GetTempPath("privoxy.log"); if (File.Exists(privoxyLogFilename)) { using (new FileStream(privoxyLogFilename, FileMode.Truncate)) { } } #else Logging.OpenLogFile(); #endif _controller = new NewShadowsocksController(); _viewController = new NewMenuViewController(_controller); HotKeys.Init(); _controller.Start(); Application.Run(); } }