Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba2d688

Browse files
committed
Safe Shutdown Steam
1 parent baba931 commit ba2d688

6 files changed

Lines changed: 34 additions & 8 deletions

File tree

src/ST.Client.CommandLine/CommandLineHost.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void MainHandlerByCLT_(Action? onInitStartuped)
163163
// -clt steam -account
164164
var steamuser = new Command("steam", "Steam 相关操作");
165165
steamuser.AddOption(new Option<string>("-account", "指定对应 Steam 用户名"));
166-
steamuser.Handler = CommandHandler.Create((string account) =>
166+
steamuser.Handler = CommandHandler.Create(async (string account) =>
167167
{
168168
if (!string.IsNullOrEmpty(account))
169169
{
@@ -182,7 +182,7 @@ void MainHandlerByCLT_(Action? onInitStartuped)
182182
steamService.SetCurrentUser(account);
183183
}
184184

185-
steamService.TryKillSteamProcess();
185+
await steamService.ShutdownSteam();
186186
steamService.StartSteam();
187187
}
188188
});

src/ST.Client.Desktop.Windows/Services/Implementation/SteamServiceImpl.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,22 @@ public bool TryKillSteamProcess()
131131
}
132132
}
133133

134-
public int? GetSteamProcessPid()
134+
public int GetSteamProcessPid()
135135
{
136136
var processes = Process.GetProcessesByName(steamProcess[0]);
137137
if (processes.Any_Nullable())
138138
return processes.First().Id;
139139
return default;
140140
}
141141

142+
private Process? GetSteamProcess()
143+
{
144+
var processes = Process.GetProcessesByName(steamProcess[0]);
145+
if (processes.Any_Nullable())
146+
return processes.First();
147+
return null;
148+
}
149+
142150
public void StartSteam(string? arguments = null)
143151
{
144152
if (!string.IsNullOrWhiteSpace(SteamProgramPath) && File.Exists(SteamProgramPath))
@@ -156,6 +164,19 @@ public void StartSteam(string? arguments = null)
156164
}
157165
}
158166

167+
public async Task ShutdownSteam()
168+
{
169+
if (!string.IsNullOrWhiteSpace(SteamProgramPath) && File.Exists(SteamProgramPath))
170+
{
171+
var steamP = GetSteamProcess();
172+
if (steamP != null)
173+
{
174+
Process2.Start(SteamProgramPath, "-shutdown", useShellExecute: true);
175+
await steamP.WaitForExitAsync();
176+
}
177+
}
178+
}
179+
159180
public string GetLastLoginUserName() => platformService.GetLastSteamLoginUserName();
160181

161182
public List<SteamUser> GetRememberUserList()

src/ST.Client/Services/ISteamService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,19 @@ public interface ISteamService
4646
/// Steam 进程是否正在运行,如果正在运行,返回进程PID提示用户去任务管理器中结束进程
4747
/// </summary>
4848
/// <returns></returns>
49-
int? GetSteamProcessPid();
49+
int GetSteamProcessPid();
5050

5151
/// <summary>
5252
/// 启动 Steam
5353
/// </summary>
5454
/// <param name="arguments"></param>
5555
void StartSteam(string? arguments = null);
5656

57+
/// <summary>
58+
/// 退出 Steam
59+
/// </summary>
60+
Task ShutdownSteam();
61+
5762
/// <summary>
5863
/// 获取最后一次自动登录 Steam 用户名称
5964
/// </summary>

src/ST.Client/UI/ViewModels/Pages/SteamAccountPageViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void OfflineModeButton_Click(SteamUser user)
126126
ReStartSteamByUser(user);
127127
}
128128

129-
private void ReStartSteamByUser(SteamUser? user = null)
129+
private async void ReStartSteamByUser(SteamUser? user = null)
130130
{
131131
foreach (var item in SteamUsers.Where(x => x.MostRecent))
132132
item.MostRecent = false;
@@ -142,7 +142,7 @@ private void ReStartSteamByUser(SteamUser? user = null)
142142
steamService.SetCurrentUser(string.Empty);
143143
}
144144

145-
steamService.TryKillSteamProcess();
145+
await steamService.ShutdownSteam();
146146
steamService.StartSteam(SteamSettings.SteamStratParameter.Value);
147147
}
148148

src/ST.Client/UI/ViewModels/Windows/GameListPage/SaveEditedAppInfoWindowViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public async Task SaveSteamEditedApps()
5151
{
5252
if (await MessageBox.ShowAsync(AppResources.SaveEditedAppInfo_RestartSteamTip, ThisAssembly.AssemblyTrademark, MessageBox.Button.OKCancel) == MessageBox.Result.OK)
5353
{
54-
ISteamService.Instance.TryKillSteamProcess();
54+
await ISteamService.Instance.ShutdownSteam();
5555
ISteamService.Instance.StartSteam();
5656
}
5757
Toast.Show(AppResources.SaveEditedAppInfo_SaveToSteamSuccess);

src/ST.Client/UI/ViewModels/Windows/SteamAccountPage/ShareManageViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public async void SetActivity_Click()
198198
var result = await MessageBox.ShowAsync(AppResources.AccountChange_RestartSteam, button: MessageBox.Button.OKCancel);
199199
if (result.IsOK())
200200
{
201-
steamService.TryKillSteamProcess();
201+
await steamService.ShutdownSteam();
202202
steamService.StartSteam();
203203
}
204204
}

0 commit comments

Comments
 (0)