RunProgramAsync

This commit is contained in:
龙腾猫跃 committed 2026-06-15 23:42:36 +08:00
1 parent a407618ee1
commit 934bf1e729
3 files changed
+66 -6

No files matched your search

+8 -6
View File
@@ -21,12 +21,6 @@
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\Plain Craft Launcher 2\Resources\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<Using Include="System.Diagnostics.CodeAnalysis" /> <Using Include="System.Diagnostics.CodeAnalysis" />
<Using Include="System.Collections" /> <Using Include="System.Collections" />
@@ -35,9 +29,17 @@
<Using Include="System.Net" /> <Using Include="System.Net" />
<Using Include="System.Runtime.CompilerServices" /> <Using Include="System.Runtime.CompilerServices" />
<Using Include="System.Runtime.InteropServices" /> <Using Include="System.Runtime.InteropServices" />
<Using Include="Newtonsoft.Json" />
<Using Include="Newtonsoft.Json.Linq" />
<Using Include="MeloongCore.Extensions" /> <Using Include="MeloongCore.Extensions" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\Plain Craft Launcher 2\Resources\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>$(DefineConstants);SNAPSHOT</DefineConstants> <DefineConstants>$(DefineConstants);SNAPSHOT</DefineConstants>
</PropertyGroup> </PropertyGroup>
+50
View File
@@ -0,0 +1,50 @@
using System.Diagnostics;
namespace MeloongCore;
public static class TaskUtils {
public static void Run(this Task task) => task.GetAwaiter().GetResult();
public static T Run<T>(this Task<T> task) => task.GetAwaiter().GetResult();
/// <summary>
/// 静默运行程序并等待其结束,返回其输出和退出码。
/// 支持 notepad、git 等命令。
/// 超时或启动失败会抛出异常。
/// </summary>
public static async Task<(string Output, int ExitCode)> RunProgramAsync(string file, string arguments = "", int? timeoutMs = null, Encoding? encoding = null) {
Logger.Info($"运行程序,并返回其输出:{file} {arguments}");
var info = new ProcessStartInfo {
Arguments = arguments,
FileName = PathUtils.ToShortPath(file),
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true, StandardOutputEncoding = encoding,
RedirectStandardError = true, StandardErrorEncoding = encoding
};
using var program = new Process { StartInfo = info, EnableRaisingEvents = true };
if (!program.Start()) throw new InvalidOperationException($"运行程序时出现意外错误:{file} {arguments}");
// 读取输出和错误流
var outputTask = program.StandardOutput.ReadToEndAsync();
var errorTask = program.StandardError.ReadToEndAsync();
var completionSource = new TaskCompletionSource<object?>(TaskCreationOptions.RunContinuationsAsynchronously);
program.Exited += (_, _) => completionSource.TrySetResult(null);
if (program.HasExited) completionSource.TrySetResult(null);
var task = completionSource.Task;
// 等待超时
if (timeoutMs.HasValue && timeoutMs > 0) {
Logger.Trace($"等待程序 {program.Id} 完成,超时:{timeoutMs.Value}ms");
if (await Task.WhenAny(task, Task.Delay(timeoutMs.Value)) != task) {
try {
if (!program.HasExited) program.Kill();
} catch (InvalidOperationException) { // 进程已退出,无需处理
}
throw new TimeoutException($"运行程序超时:{file} {arguments}");
}
}
await task;
return (await outputTask + await errorTask, program.ExitCode);
}
}
+8
View File
@@ -30,6 +30,8 @@
<Using Include="System.Net" /> <Using Include="System.Net" />
<Using Include="System.Runtime.CompilerServices" /> <Using Include="System.Runtime.CompilerServices" />
<Using Include="System.Runtime.InteropServices" /> <Using Include="System.Runtime.InteropServices" />
<Using Include="Newtonsoft.Json" />
<Using Include="Newtonsoft.Json.Linq" />
<Using Include="MeloongCore" /> <Using Include="MeloongCore" />
<Using Include="MeloongCore.Extensions" /> <Using Include="MeloongCore.Extensions" />
</ItemGroup> </ItemGroup>
@@ -38,6 +40,12 @@
<ProjectReference Include="..\Shared\MeloongCore.csproj" /> <ProjectReference Include="..\Shared\MeloongCore.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\Plain Craft Launcher 2\Resources\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />