Skip to content

Instantly share code, notes, and snippets.

@the9ball
Last active May 13, 2022 07:58
Show Gist options
  • Select an option

  • Save the9ball/dc8a245f7fca4263cab368f9e8a45739 to your computer and use it in GitHub Desktop.

Select an option

Save the9ball/dc8a245f7fca4263cab368f9e8a45739 to your computer and use it in GitHub Desktop.
dotnet script %~dp0hoge.csx 5 && echo success || echo error
if (Args.Count < 1)
{
throw new ArgumentException("秒数を指定してください");
}
var timeout = TimeSpan.FromSeconds(double.Parse(Args[0]));
// プロセス起動
var p = System.Diagnostics.Process.Start("sleep.exe", "3");
try
{
var startTime = DateTime.UtcNow;
while (!p.HasExited)
{
// なんかチェックする処理
await Task.Delay(TimeSpan.FromSeconds(1));
if (DateTime.UtcNow - startTime > timeout)
{
Console.WriteLine("Timeout");
p.Kill();
throw new TimeoutException("");
}
}
}
finally
{
if (!p.HasExited)
{
p.Kill();
await p.WaitForExitAsync();
}
}
Console.WriteLine("Complete");
return p.ExitCode;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment