I have created a single instance application using VisualBasic.dll but there are some situations where I need to create multiple instances. I needed single instance behavior to handle the context menu behavior where each windows context menu command will invoke a new instance of the application. I still need some context menus to load a another instance. I solved this by creating a new application context and running it on a new thread.
Sample code
var thread = new Thread(() => ThreadOpenFile(args));
thread.TrySetApartmentState(ApartmentState.STA);
thread.Start();
Thread content
private static void ThreadOpenFile(string[] args)
{
ApplicationContext appCnxt= new ApplicationContext(new newForm(args[1]));
Application.Run(appCnxt);
}
I its same as
private static void ThreadOpenFile(string[] args)
{
Application.Run(new newForm(args[1]));
}
This will create a new ApplicationContext, ThreadContext and the new form and Application.Run will link ThreadContext to the ApplicationContext just like starting a new application. I cannot find any documentation explaining what happens and who is managing this "new Thread" I am creating. If I try to start a new process it will invoke single instance handler
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
of VisualBasic.dll and go in a recursive loop forever as expected. I want to know whether this is the right approach or is there a better way ?
This link shows how to use application context to hook a Splash screen behavior. If there are good references I can find the internals is also appreciated.