A
A
Alexander2017-09-11 20:27:17
C++ / C#
Alexander, 2017-09-11 20:27:17

How can I open a new link in an existing WebBrowser using the command line in a previously launched program?

It is necessary in my program, in WebBrowser, to open links transmitted by a third-party program through the command line, i.e. for example "My.exe google.com ", but so that my program does not start again every time, and the page is updated in the already running WebBrowser my.exe.
I implemented the prohibition of launching the second instance of the program as follows:

C#
[STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            SingleInstanceApplication.Run(new Form1(), StartupNextInstanceHandler);
        }
        static void StartupNextInstanceHandler(object sender, StartupNextInstanceEventArgs e)
        {
         // не совсем понимаю надо ли это мне и что тут должно быть   
        }

public class SingleInstanceApplication : WindowsFormsApplicationBase
    {
        private SingleInstanceApplication()
        {
            base.IsSingleInstance = true;
        }
 
        public static void Run(Form f, StartupNextInstanceEventHandler startupHandler)
        {
            SingleInstanceApplication app = new SingleInstanceApplication();
            app.MainForm = f;
            app.StartupNextInstance += startupHandler;
            app.Run(Environment.GetCommandLineArgs());
        }
    }

Farther:
C#
public Form1()
        {
            SetBrowserFeatureControl(); // прописываются ключи в реестр 
 
            InitializeComponent();
            webBrowser1.ScriptErrorsSuppressed = true;
 
            string[] arg = Environment.GetCommandLineArgs();
            
 
            string login;
            string password;
            if (arg.Length == 2)
            {
                if (CheckURL(@arg[1]))
                {
                    webBrowser1.Navigate(@arg[1]);
                    MessageBox.Show("Должна открыться новая страница " + @arg[1]); // debug
                }
            }
// дальше другой код
        }

If you run the program with the parameter for the first time (according to the command line parameter), then everything is fine, the page opens, but if you try to run it again (by passing it a new address as a parameter), then the page is not updated. In this case, the message " A new page should open " is displayed with the desired new address, i.e. reaches this point, but webBrowser1.Navigate does not work.
I don't know what's wrong and where to look...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2017-09-11
@Adler_lug

Understood already. It was necessary to do this (the rest of the code is the same):

static void StartupNextInstanceHandler(object sender, StartupNextInstanceEventArgs e)
        {
            SingleInstanceApplication sia = (SingleInstanceApplication)sender;
            Form1 f = (Form1)sia.ApplicationContext.MainForm;
            if (e.CommandLine.Count == 2)
            {
                string link = e.CommandLine[1];
                if (f.CheckURL(link))
                {
                    f.webBrowser1.Navigate(link);
                }
            }
        }

I tried something similar, but not correct. Key to this:
SingleInstanceApplication sia = (SingleInstanceApplication)sender;
Form1 f = (Form1)sia.ApplicationContext.MainForm;

P
Peter, 2017-09-11
@petermzg

You need not to prevent the launch of the second instance, but to monitor the already running instance.
When restarted with parameters, the program checks whether the instance is still running, if it is running, it passes the necessary parameters to it and exits.
For parameter passing, study inter-process communication (Named pipes, sockets, windows window messages, shared files, etc.)

Z
Zakharov Alexander, 2017-09-11
@AlexZaharow

If you make an embedded web server in your program and set up a simple url parameter handler for it, then through these parameters you can pass any url to it in order to launch it in your program.
1. Here, offhand, people are discussing embedded web servers in c#: https://stackoverflow.com/questions/4268814/embedd...
2. Any browser can be a client for your program, in which you specify the desired url as a parameter , or write a simple client in any language just to send a request. You can use fiddler2 to test the functionality of your embedded web server.
You can combine these two properties in one program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question