0

Example illustrating my project

static async Task Main()
{
    await RunAsync("a.exe", string.Empty, string.Empty, 100000, null);
}

static async Task RunAsync(string fileName, string arguments, string workingDir, int timeLimit, string input)
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = fileName.ToString();
    startInfo.Arguments = arguments.ToString();
    startInfo.WorkingDirectory = workingDir;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    startInfo.ErrorDialog = false;
    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;

    using (Process process = new Process())
    {
        process.StartInfo = startInfo;
        StringBuilder output = new StringBuilder();
        process.OutputDataReceived += (object sendingProcess, DataReceivedEventArgs e) =>
        {
            output.AppendLine(e.Data);
        };
        StringBuilder error = new StringBuilder();
        process.ErrorDataReceived += (object sendingProcess, DataReceivedEventArgs e) =>
        {
            error.AppendLine(e.Data);
        };

        process.Start();
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();

        if (input != null)
            await process.StandardInput.WriteLineAsync(input);

        if (!process.WaitForExit(timeLimit))
            process.Kill();

        if (!string.IsNullOrWhiteSpace(error.ToString()))
        {
            return;
        }

        Console.WriteLine(output.ToString());
        return;
    }
}

I assume file a.exe hangs. Process will Kill() it when time limit. If the main thread is improperly shut down, it will always run and show up in Task Manager.

Image

Is there way for Process to self-destroy without main thread?

Tan
  • 149
  • 1
  • 9
  • What does “improperly shut down” mean? – user2864740 Dec 31 '21 at 18:52
  • Anyway, I suspect the issue relates from the child process outliving the parent process. Discussion and several approaches are given in https://stackoverflow.com/q/3342941/2864740 – user2864740 Dec 31 '21 at 18:55
  • I am developing a project on IIS, some requests make my function hang, I don't know what is the cause? – Tan Dec 31 '21 at 18:57
  • From the question linked above, given control of a.exe, I’d probably write it to terminate when the stdin (or another shared pipe) is closed, or consider the Windows Job API (https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects). – user2864740 Dec 31 '21 at 18:58
  • IIS and “hanging” could be something else.. – user2864740 Dec 31 '21 at 18:58
  • Win Job API maybe not solve my problem, all processing on task is done very well by Process, my problem is how to manage processes when they are stuck? – Tan Dec 31 '21 at 19:13
  • I'm building a compiler on the server, hanging can be caused by some incorrect requests – Tan Dec 31 '21 at 19:19
  • Is a.exe “stuck” or does it just not end in time? Maybe that is a bug in a.exe? To control the shutdown can use something like pipe control, assuming a.exe is written cooperatively .. https://stackoverflow.com/a/44432116/2864740 , https://stackoverflow.com/a/29526954/2864740 Then when the IIS request ends (ie. timeout) it can close the pipe and the a.exe (with a little bit of code changes in it) will see the pipe is closed and stop processing (end) as soon as it can. – user2864740 Dec 31 '21 at 19:30
  • That way the a.exe is always responsible to end itself (and has a chance to do any basic shutdown operations), and the lifetime is still scoped to an IIS request (or at least as long as the pipe is kept open). Then it’s a matter of scoping how the pipe is closed (perhaps it’s tied with the request cancellation token?). – user2864740 Dec 31 '21 at 19:34
  • I'm trying to do it with CancellationToken, but it not working – Tan Dec 31 '21 at 20:02

0 Answers0