Skip to content

Commit cbd2106

Browse files
author
User
committed
fix: 改进后端进程管理和安装程序
- 添加多重后端进程关闭机制 - RunEvent::Exit 时确保关闭后端 - Windows 下额外清理遗留进程 - 只生成 NSIS 安装程序(更小更快) - 配置中文安装界面
1 parent 79ac81a commit cbd2106

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src-tauri/src/lib.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,28 @@ pub fn run() {
4141
Ok(())
4242
})
4343
.on_window_event(|window, event| {
44-
if let tauri::WindowEvent::CloseRequested { .. } = event {
45-
if window.label() == "main" {
46-
stop_backend();
44+
match event {
45+
tauri::WindowEvent::CloseRequested { .. } => {
46+
if window.label() == "main" {
47+
stop_backend();
48+
}
49+
}
50+
tauri::WindowEvent::Destroyed => {
51+
if window.label() == "main" {
52+
stop_backend();
53+
}
4754
}
55+
_ => {}
4856
}
4957
})
50-
.run(tauri::generate_context!())
51-
.expect("error while running tauri application");
58+
.build(tauri::generate_context!())
59+
.expect("error while building tauri application")
60+
.run(|_app_handle, event| {
61+
// 应用退出时确保关闭后端
62+
if let tauri::RunEvent::Exit = event {
63+
stop_backend();
64+
}
65+
});
5266
}
5367

5468
fn start_backend_sidecar(app: &tauri::AppHandle) -> Result<(), String> {
@@ -93,8 +107,33 @@ fn start_backend_sidecar(app: &tauri::AppHandle) -> Result<(), String> {
93107
fn stop_backend() {
94108
if let Ok(mut guard) = BACKEND_HANDLE.lock() {
95109
if let Some(child) = guard.take() {
96-
let _ = child.kill();
97-
println!("Backend process stopped");
110+
match child.kill() {
111+
Ok(_) => println!("Backend process stopped successfully"),
112+
Err(e) => eprintln!("Failed to stop backend: {}", e),
113+
}
98114
}
99115
}
116+
117+
// 额外清理:尝试终止所有遗留的后端进程
118+
#[cfg(windows)]
119+
{
120+
let _ = std::process::Command::new("taskkill")
121+
.args(["/F", "/IM", "loarchive-backend-x86_64-pc-windows-msvc.exe"])
122+
.creation_flags(0x08000000) // CREATE_NO_WINDOW
123+
.output();
124+
}
125+
}
126+
127+
// Windows 特定:隐藏控制台窗口标志
128+
#[cfg(windows)]
129+
trait CommandExt {
130+
fn creation_flags(&mut self, flags: u32) -> &mut Self;
131+
}
132+
133+
#[cfg(windows)]
134+
impl CommandExt for std::process::Command {
135+
fn creation_flags(&mut self, flags: u32) -> &mut Self {
136+
use std::os::windows::process::CommandExt as WinCommandExt;
137+
self.creation_flags(flags)
138+
}
100139
}

src-tauri/tauri.conf.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"bundle": {
3535
"active": true,
36-
"targets": "all",
36+
"targets": ["nsis"],
3737
"icon": [
3838
"icons/32x32.png",
3939
"icons/128x128.png",
@@ -47,9 +47,17 @@
4747
"windows": {
4848
"webviewInstallMode": {
4949
"type": "embedBootstrapper"
50+
},
51+
"nsis": {
52+
"oneClick": false,
53+
"perMachine": false,
54+
"allowElevation": true,
55+
"installerIcon": "icons/icon.ico",
56+
"uninstallerIcon": "icons/icon.ico",
57+
"displayLanguageSelector": false,
58+
"languages": ["SimpChinese", "English"]
5059
}
51-
},
52-
"createUpdaterArtifacts": false
60+
}
5361
},
5462
"plugins": {
5563
"shell": {

0 commit comments

Comments
 (0)