代理修改
This commit is contained in:
@@ -6,6 +6,24 @@ use std::thread;
|
||||
use std::time::Duration;
|
||||
use tauri::{AppHandle, Emitter, Manager};
|
||||
|
||||
// Windows 平台用于隐藏控制台窗口的标志位
|
||||
// CREATE_NO_WINDOW = 0x08000000,阻止子进程创建新的控制台窗口
|
||||
#[cfg(windows)]
|
||||
pub const CREATE_NO_WINDOW: u32 = 0x08000000;
|
||||
|
||||
/// 为 Command 设置平台特定的创建标志(Windows 上隐藏控制台窗口)
|
||||
/// 公开以便其他模块(如 mihomo_manager 调用 mihomo -v 查询版本)复用
|
||||
#[cfg(windows)]
|
||||
pub fn setup_creation_flags(cmd: &mut Command) {
|
||||
use std::os::windows::process::CommandExt;
|
||||
cmd.creation_flags(CREATE_NO_WINDOW);
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
pub fn setup_creation_flags(_cmd: &mut Command) {
|
||||
// 非 Windows 平台无需处理
|
||||
}
|
||||
|
||||
/// 进程状态枚举
|
||||
#[derive(Serialize, Clone, Debug)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
@@ -94,6 +112,8 @@ impl ProcessManager {
|
||||
cmd.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.stdin(Stdio::null());
|
||||
// Windows 上隐藏控制台窗口(mihomo.exe 是控制台程序,否则会弹黑框)
|
||||
setup_creation_flags(&mut cmd);
|
||||
|
||||
let child = cmd
|
||||
.spawn()
|
||||
@@ -123,6 +143,8 @@ impl ProcessManager {
|
||||
}
|
||||
|
||||
/// 停止指定进程
|
||||
/// kill 在主线程执行(快速),wait 移到后台线程执行避免阻塞前端
|
||||
/// Windows 上 kill 后 wait 可能需要等待子进程清理资源,有几十毫秒到几百毫秒延迟
|
||||
pub fn stop(&self, id: &str) -> Result<(), String> {
|
||||
let mut processes = self.processes.lock().map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -131,7 +153,11 @@ impl ProcessManager {
|
||||
.child
|
||||
.kill()
|
||||
.map_err(|e| format!("终止进程 '{}' 失败: {}", id, e))?;
|
||||
let _ = entry.child.wait();
|
||||
// 后台等待子进程退出,避免阻塞当前调用线程(前端会感知卡顿)
|
||||
// child 已 move 进闭包,wait 在后台完成
|
||||
thread::spawn(move || {
|
||||
let _ = entry.child.wait();
|
||||
});
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("进程 '{}' 不存在", id))
|
||||
@@ -240,6 +266,7 @@ impl ProcessManager {
|
||||
cmd.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.stdin(Stdio::null());
|
||||
setup_creation_flags(&mut cmd);
|
||||
|
||||
match cmd.spawn() {
|
||||
Ok(new_child) => {
|
||||
|
||||
Reference in New Issue
Block a user