Answer the question
In order to leave comments, you need to log in
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:
[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());
}
}
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
}
}
// дальше другой код
}
Answer the question
In order to leave comments, you need to log in
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);
}
}
}
SingleInstanceApplication sia = (SingleInstanceApplication)sender;
Form1 f = (Form1)sia.ApplicationContext.MainForm;
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.)
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 questionAsk a Question
731 491 924 answers to any question