Home Articles Windows presentation foundation Single instance application

Tips and tricks

Site statistics

Members : 19
Content : 104
Content View Hits : 18542

Who's Online

We have 12 guests online
Single instance application PDF Print E-mail
User Rating: / 0
PoorBest 
Articles - Windows presentation foundation
Written by Dot4Pro   
Tuesday, 15 December 2009 03:45

Single instance application in wpf

What is single instance application:

Only one instance of the application runs once at a time on a single machine.

Need of single instance application:

If your application is so heavy and takes lots of system resources, and running multiple instance is not required, preventing users to run only one instance of the application can help a lot.

There are lots of methods to restrict to run multiple instance of application in wpf.

Method 1:

Override the Startup method on the App class(App.xaml.cs)  and check if process of the same name is already running or not. If same process name already running  show the message to notify user that another instance of the application is already running and exit the process.

Code sample:

  protected override void OnStartup(StartupEventArgs e)         {             Process thisProc = Process.GetCurrentProcess();             if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)             {                 MessageBox.Show("Application is already running.");                 Application.Current.Shutdown();                 return;             }             base.OnStartup(e);                      }  

Method 2:

Using WindowsFormsApplicationBase .
Create a class inheriting WindowsFormsApplicationBase.
You must reference VisualBasic ApplicationServices assembly.
Since you must create your own main method, you have to remove App.xaml and App.xaml.cs files.

Code sample:

 public class SingleInstanceApp : WindowsFormsApplicationBase

{

private MainApplication app;

public SingleInstanceApp()

{

IsSingleInstance = true;

}

protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)

{

app = new MainApplication();

app.Run();

return false;

}

protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e)

{

base.OnStartupNextInstance(e);

app.Activate();

}

[STAThread]

public static void Main(string[] args)

{

var splash = new SplashScreen("images/splashimage.png");

splash.Show(true);

var wrapper = new SingleInstanceApp();

wrapper.Run(args);

}

}

public class MainApplication : Application

{

protected override void OnStartup(System.Windows.StartupEventArgs e)

{

base.OnStartup(e);

StartupUri = new System.Uri("view/MainWindow.xaml", UriKind.Relative);

LoadResource("mystyles.xaml");

}

protected override void OnLoadCompleted(System.Windows.Navigation.NavigationEventArgs e)

{

base.OnLoadCompleted(e);

}

private static void LoadResource(string path)

{

Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri(path, UriKind.Relative)) as ResourceDictionary);

}

public void HandleCommandLine(System.Collections.ObjectModel.ReadOnlyCollection<string> e)

{

}

public void Activate()

{

this.MainWindow.Activate();

this.MainWindow.WindowState = WindowState.Maximized;

}

} 

Last Updated on Tuesday, 15 December 2009 04:06
 

Add comment


Security code
Refresh

feed-image Feed Entries