WPF drip (2) create a single instance application, wpf drip
Recently, a colleague asked me how to ensure that the new application is no longer started, but the previously started process is not restarted after the application is started, essentially, this is a single-instance WPF application. There is an App in the VS project tree. xaml and App. xaml. cs (both files are automatically generated by VS) in the App. xaml. cs defines the App class, which inherits from System. windows. application, from the class name, it is easy to see that these classes are related to Application management. Before we talk about creating a single-instance Application, we should first understand the Application class.
In the previous article on WPF (1) The Main function partially introduced the Application class. The Application class encapsulates the WPF Application and manages its lifecycle. The following code is used to create an Application:
Application app = new Application ();
TestWindow window = new TestWindow ();
App. MainWindow = window;
Window. Show ();
App. Run ();
The above Code creates an Application and sets the main window of the Application as TestWindow. You can simplify the above Code to the following code for the same purpose:
Application app = new Application ();
TestWindow window = new TestWindow ();
App. Run (window );
Note that another method is used in the automatically generated App class of VS to initialize the Application and main window, and the main window is set through the resource path,
Application app = new Application ();
App. StartupUri = new Uri ("TestWindow. xaml", UriKind. Relative );
App. Run ();
The preceding section describes how to start a WPF Application through code. The following describes several common scenarios of the Application class:
1. the initialization and cleaning before the application starts and exits. Here, the Startup and Exit events are used, or the OnStartup and OnExit events are reloaded. These two methods are equivalent, triggered when the program starts and exits;
2. to capture unhandled exceptions of an application, DispatcherUnhandledException. The exception handling principle is proximity capture and proximity handling. This event is mostly used to handle unexpected exceptions, it is not recommended to rely too much on Exception Handling in this event to prevent the last layer of protection from program crash. Note thatThis event only captures exceptions in the main thread (UI thread)Does not trigger this event if an unhandled exception occurs in the background thread;
3. Access the UI in the background thread, Application. Current. Dispatcher. Invoke (). This method is relatively simple, and another method is through SynchronizationContext, which is more flexible.
Now, let's get started. How to create a single-instance application is as follows, the direct method is to check whether another started application exists in the event handler function of Startup, and end the current program if yes. This method can achieve the effect of a single instance application, but it is too violent, and strictly speaking, it is not completely a single instance. After all, the application has been started, although it has been killed again.
The following describes the recommended methods of WPF. the built-in support provided by Windows forms, with the help of the WindowsFormsApplicationBase class, specifies that the application is in the single-instance mode "IsSingleInstance = true ",
Public class SingleInstanceApplication: WindowsFormsApplicationBase
{
Private App m_App;
Public SingleInstanceApplication ()
{
IsSingleInstance = true;
}
Protected override bool OnStartup (StartupEventArgs eventArgs)
{
M_App = new App
{
StartupUri = new System. Uri ("MainWindow. xaml", System. UriKind. Relative)
};
M_App.Run ();
Return false;
}
Protected override void OnStartupNextInstance (StartupNextInstanceEventArgs eventArgs)
{
Base. OnStartupNextInstance (eventArgs );
}
}
Override the Main function (For details, refer to WPF (1) Main Function). We can see that SingleInstanceApplication acts as the wrapper of the App, starts the application through SingleInstanceApplication, and controls the single instance. SingleInstanceApplication. onStartup () is triggered when the application is started for the first time. SingleInstanceApplication. onStartupNextInstance () is triggered when the application is started and the application is started again. This design is designed to achieve the effect similar to Word. Only one running application exists in Word. Each double-click operation does not start a new Word program, but opens a new window, the document path is passed through the "StartupEventArgs eventArgs" and "StartupNextInstanceEventArgs eventArgs" command line parameters.
Public class StartUp
{
[STAThread]
Public static void Main (string [] args)
{
SingleInstanceApplication application = new SingleInstanceApplication ();
Application. Run (args );
}
}
WPF does not support the single-instance mode. The above method is actually a design feature from VB. The namespace of WindowsFormsApplicationBase is Microsoft. visualBasic. applicationServices. To use this class, you need to add. visualBasic. dll reference.