chromelyapps / Chromely

Build Cross Platform HTML Desktop Apps on .NET using native GUI, HTML5, JavaScript, CSS, Owin, AspNetCore (MVC, RazorPages, Blazor)
MIT License
2.98k stars 279 forks source link

defunct child process with ChromelyApp on linux #253

Closed jirinalepa closed 3 years ago

jirinalepa commented 4 years ago

Can be reproduced on CrossPlatDemo Project. Started processes ends correctly, until Chromely starts.

Just insert this code to Program.cs before AppBuilder.....Run().

var tokenSource = new CancellationTokenSource();
var task = Task.Run(async () =>
{
    var cmd = new string[] { "ls", "-l" };
    while (!tokenSource.Token.IsCancellationRequested)
    {
        try
        {
            cmd.RunShell();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        await Task.Delay(1000);
    }
});

And RunShell() is defined as:

public static string RunShell(this IEnumerable cmdAndArgs, int timeout = 5000)
{
    var result = string.Empty;
    var cmd = cmdAndArgs.FirstOrDefault();

    if (string.IsNullOrWhiteSpace(cmd))
        return string.Empty;

    using (var process = new Process())
    {
        process.EnableRaisingEvents = true;
        process.StartInfo = new ProcessStartInfo
        {
            FileName = cmd,
            RedirectStandardOutput = true,
            UseShellExecute = false
        };
        foreach (var arg in cmdAndArgs.Skip(1))
            process.StartInfo.ArgumentList.Add(arg);
        process.Start();

        result = process.StandardOutput.ReadToEnd();
        if (timeout > 0)
        {
            if (!process.WaitForExit(timeout))
                Console.WriteLine($"ERR: '{string.Join(' ', cmdAndArgs)}' process doesn't exited.");
            else
                Console.WriteLine($"OKK: '{string.Join(' ', cmdAndArgs)}' exited properly.");
        }
        else
        {
            process.WaitForExit();
        }
        process.Kill(true);
    }

    return result;
}
mattkol commented 4 years ago

@jirinalepa this is not directly related to Chromely. CEF is a multi-process, so what you are trying to do may be impacted by that. What we have done with mvc and single instance may be helpful: web-chromely-mvc Single Instance

jirinalepa commented 3 years ago

Thanks for the directions. Although it did not completely solve the problem, at least it inspired me to find a workaround.