{"record":{"id":"8c828bf0a244308d","repo":"Uahh/ToastFish","slug":"n-toastfish","errorCode":null,"errorMessage":"程序已经在运行了，不能运行两次。\\n如果右下角软件已经退出，请在任务管理器中结束ToastFish任务。","messagePattern":"程序已经在运行了，不能运行两次。\\\\n如果右下角软件已经退出，请在任务管理器中结束ToastFish任务。","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"info","filePath":"View/ToastFish.xaml.cs","lineNumber":105,"sourceCode":"                    break;\n                default:\n                    break;\n            }           \n        }\n\n        private void Form_Load()\n\n        {\n            //获取当前活动进程的模块名称\n            string moduleName = Process.GetCurrentProcess().MainModule.ModuleName;\n            //返回指定路径字符串的文件名\n            string processName = System.IO.Path.GetFileNameWithoutExtension(moduleName);\n            //根据文件名创建进程资源数组\n            Process[] processes = Process.GetProcessesByName(processName);\n            //如果该数组长度大于1，说明多次运行\n            if (processes.Length > 1)\n            {\n                System.Windows.Forms.MessageBox.Show(\"程序已经在运行了，不能运行两次。\\n如果右下角软件已经退出，请在任务管理器中结束ToastFish任务。\", \"提示\", MessageBoxButtons.OK, MessageBoxIcon.Information);//弹出提示信息\n                this.Close();//关闭当前窗体\n            }\n        }\n\n        private void SetNotifyIcon()\n        {\n            Vm.notifyIcon = new NotifyIcon();\n            Vm.notifyIcon.Text = \"ToastFish\";\n            System.Drawing.Icon icon = IconChika.chika16;\n\n            Vm.notifyIcon.Icon = icon;\n            Vm.notifyIcon.Visible = true;\n            Vm.notifyIcon.DoubleClick += Begin_Click;\n            //Vm.notifyIcon.DoubleClick += NotifyIconDoubleClick;\n        }\n\n        public void PlayMute()\n        {","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/Uahh/ToastFish/blob/2508784a6650862a60e1f3ec3a54dd83efdec98b/View/ToastFish.xaml.cs#L87-L123","documentation":"In Form_Load (View/ToastFish.xaml.cs:105), called from the MainWindow constructor, the app derives its own executable name and calls Process.GetProcessesByName; if more than one process with that name exists, it concludes a second instance was started and shows this MessageBox before closing the current window. This is a single-instance guard meant to prevent duplicate tray icons and conflicting SQLite access — the second launch is intentionally terminated.","triggerScenarios":"Launching ToastFish.exe while another instance is already running (tray icon still alive after window was 'closed'), or when a stale process with the same executable name is running (e.g. a hung instance, a renamed copy, or the app started twice at login).","commonSituations":"User clicks the exe again thinking the app exited because the window closed while the tray process persists; two shortcuts in the Startup folder; app pinned and also auto-started; a frozen previous instance that must be killed via Task Manager as the message itself instructs; running a copied/renamed exe that collides with the original name.","solutions":["Do nothing extra: the current (duplicate) instance closes itself. Use the already-running instance's tray icon.","If no live instance is visible, open Task Manager, end the ToastFish task as the dialog instructs, then relaunch.","Run \"taskkill /IM ToastFish.exe /F\" (adjust to the exe name) to clear stale/hung instances, then start the app once.","Check Startup entries (Task Manager > Startup, shell:startup) and remove duplicates so only one auto-start exists.","Prefer a named Mutex single-instance check over process-name counting in code, since name-based detection misfires with renamed copies."],"exampleFix":"// before\nif (processes.Length > 1) { MessageBox.Show(\"程序已经在运行了...\"); this.Close(); }\n// after\nprivate static Mutex _mutex;\n_mutex = new Mutex(true, \"ToastFish.SingleInstance\", out bool createdNew);\nif (!createdNew) { MessageBox.Show(\"程序已经在运行了，不能运行两次。\"); this.Close(); return; }","handlingStrategy":"validation","validationCode":"// check for an existing instance before initializing UI\nusing var mutex = new Mutex(true, \"ToastFish.SingleInstance\", out bool createdNew);\nif (!createdNew)\n{\n    System.Windows.Forms.MessageBox.Show(\"程序已经在运行了，不能运行两次。\\n如果右下角软件已经退出，请在任务管理器中结束ToastFish任务。\", \"提示\");\n    return; // abort startup\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    // single-instance check in Program.Main / Form_Load\n}\ncatch (Exception ex)\n{\n    // process enumeration can fail under restricted environments\n    System.Diagnostics.Debug.WriteLine($\"single-instance check failed: {ex.Message}\");\n}","preventionTips":["Remember that closing the window does not exit the app — check the system tray first.","Avoid double-adding the exe to Startup entries.","Use a named Mutex instead of Process.GetProcessesByName for reliable single-instance enforcement.","If a previous instance hangs, kill it via Task Manager or taskkill before relaunching."],"tags":["single-instance","process","windows","mutex","csharp"],"backgroundTag":"address-already-in-use","analyzedSha":"2508784a6650862a60e1f3ec3a54dd83efdec98b","analyzedAt":"2026-09-15T23:21:32.897Z","contentChangedAt":"2026-09-15T23:21:32.897Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}