12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System.Threading;
- namespace SinMaiLauncher.ChildProcessHolder;
- internal class ChildProcessStateBagForConsole(ChildProcessKind kind, IProcessStartInfo startInfo) : ChildProcessStateBag(kind, startInfo)
- {
- public nint? HWndConsole { get; private set; }
- protected override async Task<bool> CheckReadyAsync()
- {
- if (Pid.HasValue == false) return false;
- const int timeoutSecond = 5;
- var startTime = DateTime.Now;
- while (IsAlive && DateTime.Now - startTime < TimeSpan.FromSeconds(timeoutSecond))
- {
- var h = Program.GetConsoleWindow(Pid.Value);
- if (h == -1)
- {
- await Task.Delay(100);
- }
- else
- {
- HWndConsole = h;
- break;
- }
- }
- Status = ChildProcessStatus.WaitingReady;
- return HWndConsole != null;
- }
- public override async Task StopAsync(TimeSpan timeout)
- {
- Status = ChildProcessStatus.Stopping;
- if (IsAlive && Pid.HasValue) Program.SendCtrlC(Pid.Value);
- await Task.WhenAny(WaitForExitAsync(), Task.Delay(timeout));
- await base.StopAsync(timeout);
- HWndConsole = null;
- }
- }
|