How to pass command line argument to MyApp

Having problems with building or using the CefGlue .NET/Mono binding? Ask your questions here.

Moderator: fddima

How to pass command line argument to MyApp

Postby ThatDude » Mon Feb 06, 2017 6:24 pm

What is the recommended way to pass command line arguments to my console headless app using CEF3 (Xilium.CefGlue)?
Additional question - why is my static void Main(string[] args) executed twice?

When i run my app without arguments - everything is fine
When I add 1 argument (file name with some data) it starts failing
in pseudo code:

Code: Select all
// non-working part using arguments
Console.WriteLine("arguments " + args.Length + string.Join(",", args)); // obviously my console app is executed twice?!
if (args.Length != 1)
    Environment.Exit(-1); // incorrect number of arguments
// if I exit here (second time around because first time there is a single argument) my browser won't even start

The code above runs twice!? First time it prints 1 - MyFile.txt second time it prints only CEF arguments 17 - ... and original MyFile.txt is lost
NB! If I remove Exit(-1) browser runs as expected, but if I keep it browser won't start at all.
I know that my console app will start separate threads (or processes for e.g. rendering) but this time it is the other way around some (I suspect renderer) thread re-executes my code?! alas without my (original) command line parameters

Code: Select all
CefRuntime.Load(); // the code below works as long as I do not use args[] above
//...
bool done = task.Browser.Completed.WaitOne(35000);
//...
CefRuntime.Shutdown();


NB! I'm not trying to pass arguments from the browser process to the renderer process - I am trying to read some data (JavaScript to execute after web page is loaded) from a file but something (within CefGlue) restarts my console app while dropping all command line parameters..

I stumbled upon some similar cases - all unsolved, but other people are experiencing the same
viewtopic.php?f=6&t=14360
viewtopic.php?f=14&t=14476
ThatDude
Newbie
 
Posts: 3
Joined: Mon Feb 06, 2017 5:45 pm

Re: How to pass command line argument to MyApp

Postby ThatDude » Tue Feb 07, 2017 1:42 pm

I figured it out - I set SingleProcess = false but failed to set BrowserSubprocessPath so browser executable was used instead, acting as if SingleProcess = true and starting my executable again for the sub-process

https://bitbucket.org/chromiumembedded/ ... executable

Now since SingleProcess run mode is not officially supported by Chromium and is less stable than the multi-process default
and I want MultiThreadedMessageLoop = true because my console app doesn't have WinMain/MessagePump/MessageLoop
so I can't use the separate-sub-process-executable example here
https://bitbucket.org/chromiumembedded/ ... executable
what is the appropriate way to run in a separate sub-process?
NB! Because of MultiThreadedMessageLoop I can't run CefRunMessageLoop(); as per documentation
so what shall I put between CefInitialize() and CefShutdown() to wait until separate-sub-process-executable finishes?
ThatDude
Newbie
 
Posts: 3
Joined: Mon Feb 06, 2017 5:45 pm

Re: How to pass command line argument to MyApp

Postby magreenblatt » Tue Feb 07, 2017 1:45 pm

Explicitly running the message loop is only done in the browser process. In sub-processes you just call the CefExecuteProcess function and then exit after it returns.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: How to pass command line argument to MyApp

Postby ThatDude » Tue Feb 07, 2017 9:46 pm

magreenblatt wrote:Explicitly running the message loop is only done in the browser process. In sub-processes you just call the CefExecuteProcess function and then exit after it returns.

Explicitly running the message loop is forbidden when using MultiThreadedMessageLoop and as far as I understand CefExecuteProcess doesn't block in main app so it will exit immediately, so how to block or wait until sub-process returns if I'm not "Explicitly running the message loop" in main app?
so I think my only option is to replace CefRunMessageLoop(); with something like:
Code: Select all
Sleep(3000); // wait until timeout
CloseBrowserGracefully(); // browser.Stop()/browser.Close() - will these execute synchronously
// do I have to close gracefully or just exit main app and it will kill sub-process anyway

Could you elaborate a bit more:
  • what is sub-process' job?
    I guess since CefInitialize() is called in main app I guess that's where I should set proxy, URL, JavaScript to be executed on page load etc. then what is the purpose of the sub-process
  • How to split MyApp into two - there are two instances one in main app and another in sub-process
    CefRefPtr<MyApp> app(new MyApp);
    • what should stay in mainApp
    • what should go in subprocessApp
    • do the two MyApp instances communicate
ThatDude
Newbie
 
Posts: 3
Joined: Mon Feb 06, 2017 5:45 pm

Re: How to pass command line argument to MyApp

Postby amaitland » Tue Feb 07, 2017 10:41 pm

Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am


Return to CefGlue Forum

Who is online

Users browsing this forum: No registered users and 17 guests