You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Program.cs 2.9 kB

10 years ago
10 years ago
10 years ago
12 years ago
10 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using Shadowsocks.Controller;
  7. using Shadowsocks.Util;
  8. using Shadowsocks.View;
  9. namespace Shadowsocks
  10. {
  11. static class Program
  12. {
  13. /// <summary>
  14. /// 应用程序的主入口点。
  15. /// </summary>
  16. [STAThread]
  17. static void Main()
  18. {
  19. Utils.ReleaseMemory(true);
  20. using (Mutex mutex = new Mutex(false, "Global\\Shadowsocks_" + Application.StartupPath.GetHashCode()))
  21. {
  22. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  23. Application.EnableVisualStyles();
  24. Application.SetCompatibleTextRenderingDefault(false);
  25. if (!mutex.WaitOne(0, false))
  26. {
  27. Process[] oldProcesses = Process.GetProcessesByName("Shadowsocks");
  28. if (oldProcesses.Length > 0)
  29. {
  30. Process oldProcess = oldProcesses[0];
  31. }
  32. MessageBox.Show(I18N.GetString("Find Shadowsocks icon in your notify tray.") + "\n" +
  33. I18N.GetString("If you want to start multiple Shadowsocks, make a copy in another directory."),
  34. I18N.GetString("Shadowsocks is already running."));
  35. return;
  36. }
  37. Directory.SetCurrentDirectory(Application.StartupPath);
  38. #if DEBUG
  39. Logging.OpenLogFile();
  40. // truncate privoxy log file while debugging
  41. string privoxyLogFilename = Utils.GetTempPath("privoxy.log");
  42. if (File.Exists(privoxyLogFilename))
  43. using (new FileStream(privoxyLogFilename, FileMode.Truncate)) { }
  44. #else
  45. Logging.OpenLogFile();
  46. #endif
  47. ShadowsocksController controller = new ShadowsocksController();
  48. MenuViewController viewController = new MenuViewController(controller);
  49. controller.Start();
  50. Application.Run();
  51. }
  52. }
  53. private static int exited = 0;
  54. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  55. {
  56. if (Interlocked.Increment(ref exited) == 1)
  57. {
  58. Logging.Error(e.ExceptionObject?.ToString());
  59. MessageBox.Show(I18N.GetString("Unexpect error, shadowsocks will be exit. Please report to") +
  60. " https://github.com/shadowsocks/shadowsocks-windows/issues " +
  61. Environment.NewLine + (e.ExceptionObject?.ToString()),
  62. "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  63. Application.Exit();
  64. }
  65. }
  66. }
  67. }