1

I have a basic WebView2 browser where I'm trying to open URLs in the same instance via the Run command. This would be similar to opening a webpage on Chrome via the Run command (e.g. chrome https://www.google.com) however each run command will need to open the URL in the same WebView2 browser instance.

I have tried running the following command on my WebView2 browser:

"C:\Project\WebView\bin\Release\WebView2Browser.exe" https://www.google.com

However, I've run into the following issues:

  1. Running the command does not open Google in the WebView2 browser.
  2. Each time the command is run, it opens a new instance of the WebView2 browser.

What am I missing?

Sam
  • 27
  • 4
  • I (or we?) are missing the source code for this WebView2Browser. – Luuk May 19 '23 at 07:51
  • [WebView2Browser shows some of the simplest uses of WebView2](https://github.com/MicrosoftEdge/WebView2Browser/blob/main/README.md), so you can change the code provided on GitHub, to make it do what you like. – Luuk May 19 '23 at 08:17
  • 1
    When you run an exe it will start a new process. The application has to do work to detect that there is already another instance of the same application running and to forward any startup parameters to the already running application. WebView2Browser does not appear to implement this. Similarly WebView2Browser does not appear to examine its command line parameters at all. – David Risney May 22 '23 at 17:47

1 Answers1

0

By the sounds of it, you need to get to grips with the application mutex concept. So:

  1. when your application starts it tries to create a unique (eg a constant GUID).
  2. if it fails to create the mutex it means the application is already running.
  3. you locate that running application instance and post the command line string to it.
  4. your running instance detects the message with the data, and handles it accordingly.
  5. bring the running instance to the foreground
  6. terminate the duplicate instance.

(note: the above terms are based on Visual C++, and may be different for C#, but the principles should be the same).


I do something similar in my Visual C++ MFC application where a user double clicks a MWB or SRR data file. It tries to start my app, and since it is running, passes the file info over to the running instance to process and brings it to the foreground.

I would show you some code but it is for Visual C++ MFC and not C#, but you should be able to find a lot of documentation about this process.

Reference Material

These discussions are tagged C#:

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164