Skip to content

Commit

Permalink
Close CefSharp.BrowserSubprocess.exe if parent process exits (#2375)
Browse files Browse the repository at this point in the history
* Close cefsharp subprocess if main process dies

* Code review

* Await before exiting

* Update Program.cs
  • Loading branch information
João Neves authored and amaitland committed Jun 20, 2018
1 parent a9a8cb8 commit 4f9c95c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Expand Down
24 changes: 23 additions & 1 deletion CefSharp.BrowserSubprocess/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using CefSharp.Internals;

namespace CefSharp.BrowserSubprocess
Expand All @@ -19,6 +20,7 @@ public static int Main(string[] args)
int result;
var type = args.GetArgumentValue(CefSharpArguments.SubProcessTypeArgument);
var parentProcessId = int.Parse(args.GetArgumentValue(CefSharpArguments.HostProcessIdArgument));
Task.Factory.StartNew(() => AwaitParentProcessExit(parentProcessId), TaskCreationOptions.LongRunning);

//Use our custom subProcess provides features like EvaluateJavascript
if (type == "renderer")
Expand All @@ -37,8 +39,28 @@ public static int Main(string[] args)
}

Debug.WriteLine("BrowserSubprocess shutting down.");

return result;
}

private static async void AwaitParentProcessExit(int parentProcessId)
{
try
{
var parentProcess = Process.GetProcessById(parentProcessId);
parentProcess.WaitForExit();
}
catch (Exception e)
{
//main process probably died already
Debug.WriteLine(e);
}

await Task.Delay(1000); //wait a bit before exiting

Debug.WriteLine("BrowserSubprocess shutting down forcibly.");

Environment.Exit(0);
}
}
}
5 changes: 2 additions & 3 deletions CefSharp/CefCustomScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,12 @@ public CefCustomScheme()
/// <returns>list of scheme objects</returns>
public static List<CefCustomScheme> ParseCommandLineArguments(IEnumerable<string> args)
{
var schemes = args.FirstOrDefault(a => a.StartsWith(CefSharpArguments.CustomSchemeArgument));
var schemes = args.GetArgumentValue(CefSharpArguments.CustomSchemeArgument);
var customSchemes = new List<CefCustomScheme>();

if (!string.IsNullOrEmpty(schemes))
{
//Remove the "--custom-scheme=" part of the argument
schemes.Substring(CefSharpArguments.CustomSchemeArgument.Length + 1).Split(';').ToList().ForEach(x =>
schemes.Split(';').ToList().ForEach(x =>
{
var tokens = x.Split('|');
var customScheme = new CefCustomScheme
Expand Down

0 comments on commit 4f9c95c

Please sign in to comment.