Today we are going to implement the single instance in wpf.
That is the created WPF application is opened only once.
The code for the App.xaml is
That is the created WPF application is opened only once.
The code for the App.xaml is
<Application x:Class="MyProject.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Startup="Application_Startup" > </Application>
and the code behind for the Application_Startup is
private void Application_Startup(object sender, StartupEventArgs e) { var appProcessName = Process.GetCurrentProcess().ProcessName; var matchingProcesses = Process.GetProcessesByName(appProcessName); if (matchingProcesses.Count() == 1) { MainWindow mainWindow = new MainWindow(); mainWindow.Show(); } else { Application.Current.Shutdown(); } }
When the above code implemented, Rebuild and create the application setup now run the application many times but it will be open only once.
No comments:
Post a Comment