In this blog I will show how to implement an single instance application:
To implement, I will use Microsoft.Visualbasic library (latest version is 10.0.0.0).
In your main project, create a class inherit from WindowsFormsApplicationBase
public class SingleApp : WindowsFormsApplicationBase
{
{
//this is the actual App class (App.xaml)
private App _app;
public SingleApp()
{
IsSingleInstance = true;
private App _app;
public SingleApp()
{
IsSingleInstance = true;
}
protected override bool OnStartup(StartupEventArgs eventArgs)
{
try
{
_app = new App();
_app.Run();
}
catch (Exception ex)
{
//do handle exception
protected override bool OnStartup(StartupEventArgs eventArgs)
{
try
{
_app = new App();
_app.Run();
}
catch (Exception ex)
{
//do handle exception
}
return false;
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
return false;
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
//make the running application do something. Show main window for exsample
_app.NextInstanceRun();
}
}
_app.NextInstanceRun();
}
}
SingleApp will act like a Application host.
Now we create a static Main() function to run this SingleApp
public class AppAccessPoint
{ /// <summary> /// Mains the specified args. /// This is the real entry point, not the WPF APP /// </summary> /// <param name="args">The args.</param> [STAThread] public static void Main(string[] args) {
SingleApp manager = new SingleApp(); manager.Run(args); } }
make sure to change the start up project to the Main function
Done
No comments:
Post a Comment