mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
fix(tray): resolve "Restart App" failure on Windows (#4960)
* fix(tray): resolve "Restart App" failure on Windows * style: rm useless comment
This commit is contained in:
@@ -26,7 +26,6 @@ pub async fn restart_clash_core() {
|
|||||||
|
|
||||||
/// Restart the application
|
/// Restart the application
|
||||||
pub async fn restart_app() {
|
pub async fn restart_app() {
|
||||||
// Step 1: Perform cleanup and check for errors
|
|
||||||
if let Err(err) = resolve::resolve_reset_async().await {
|
if let Err(err) = resolve::resolve_reset_async().await {
|
||||||
handle::Handle::notice_message(
|
handle::Handle::notice_message(
|
||||||
"restart_app::error",
|
"restart_app::error",
|
||||||
@@ -36,43 +35,41 @@ pub async fn restart_app() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Attempt to get app handle and restart
|
handle::Handle::notice_message("restart_app::info", "Restarting application...");
|
||||||
match handle::Handle::global().app_handle() {
|
|
||||||
Some(app_handle) => {
|
// Use the manual restart method consistently to ensure reliability across platforms
|
||||||
handle::Handle::notice_message("restart_app::info", "Restarting application...");
|
// This addresses the issue where app_handle.restart() doesn't work properly on Windows
|
||||||
app_handle.restart();
|
let current_exe = match env::current_exe() {
|
||||||
|
Ok(path) => path,
|
||||||
|
Err(_) => {
|
||||||
|
// If we can't get the current executable path, try to use the fallback method
|
||||||
|
if let Some(app_handle) = handle::Handle::global().app_handle() {
|
||||||
|
app_handle.restart();
|
||||||
|
}
|
||||||
|
exit(1); // If we reach here, either app_handle was None or restart() failed to restart
|
||||||
}
|
}
|
||||||
None => {
|
};
|
||||||
handle::Handle::notice_message(
|
|
||||||
"restart_app::error",
|
|
||||||
"Failed to get app handle for restart",
|
|
||||||
);
|
|
||||||
logging_error!(
|
|
||||||
Type::System,
|
|
||||||
false,
|
|
||||||
"{}",
|
|
||||||
"Failed to get app handle for restart"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Fallback: launch a new instance of the application and exit the current one
|
let mut cmd = Command::new(current_exe);
|
||||||
let current_exe = env::current_exe().unwrap_or_else(|_| {
|
cmd.args(env::args().skip(1));
|
||||||
exit(1); // Exit if can't find the executable path
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut cmd = Command::new(current_exe);
|
match cmd.spawn() {
|
||||||
cmd.args(env::args().skip(1));
|
Ok(child) => {
|
||||||
|
log::info!(target: "app", "New application instance started with PID: {}", child.id());
|
||||||
match cmd.spawn() {
|
// Successfully started new process, now exit current process
|
||||||
Ok(child) => {
|
if let Some(app_handle) = handle::Handle::global().app_handle() {
|
||||||
log::info!(target: "app", "New application instance started with PID: {}", child.id());
|
app_handle.exit(0);
|
||||||
// Successfully started new process, now exit current process
|
} else {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
}
|
||||||
log::error!(target: "app", "Failed to start new application instance: {}", e);
|
Err(e) => {
|
||||||
// Unable to start new process, exit with error
|
log::error!(target: "app", "Failed to start new application instance: {}", e);
|
||||||
exit(1);
|
// If manual spawn fails, try the original restart method as a last resort
|
||||||
}
|
if let Some(app_handle) = handle::Handle::global().app_handle() {
|
||||||
|
app_handle.restart();
|
||||||
|
} else {
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user