Search code examples
c#webview2

WebView2: Targetting a URL via Run command in the same instance


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?


Solution

  • 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#: