WPF Single Instance workaround

Source: Internet
Author: User

WPF itself does not support the built-in standalone mode (it will be supported in later versions), while the WindowsFormsApplicationBase class supports the standalone mode (the full name is Microsoft. visualBasic. applicationServices. windowsFormsApplicationBase), so we can use WindowsFormsApplicationBase as a wrapper to implement the standalone mode.
There are three knowledge points for implementing a single program:

(*) The IsSingleInstance property enables a single-instance application. You set this property
To true in the constructor.
(*) The OnStartup () method is triggered when the application starts. You override this
Method and create the WPF application object at this point.
(*) The OnStartupNextInstance () method is triggered when another instance of the application
Starts up.

In addition, pay attention to the following points during implementation:

(*) You need to add an Assembly reference for Microsoft. VisualBasic. dll.

(*) The application needs to start with a traditional Main () method, rather than an App. xaml file.

Code Implementation

Add a new file, for example, Startup. cs. Add the following code in it:

Namespace your namespace
{
Public class Startup
{
[STAThread]
Public static void Main (string [] args)
{
SingleInstanceApplicationWrapper wrapper = new SingleInstanceApplicationWrapper ();
Wrapper. Run (args );
}
}

Public class SingleInstanceApplicationWrapper:
Microsoft. VisualBasic. ApplicationServices. WindowsFormsApplicationBase
{
Private App app; // This is the real WPF Application

Public SingleInstanceApplicationWrapper ()
{
This. IsSingleInstance = true;
}

// Call this method for the first time
Protected override bool OnStartup (
Microsoft. VisualBasic. ApplicationServices. StartupEventArgs e)
{
App = new App ();
App. Run ();

Return false;
}

// Call this method again
Protected override void OnStartupNextInstance (
Microsoft. VisualBasic. ApplicationServices. StartupNextInstanceEventArgs e)
{
// When the user tries to open the program again
MessageBox. Show ("You are running this program ");
}
}

/// <Summary>
/// Interaction logic for App. xaml
/// </Summary>
Public partial class App: Application
{
}
}

Note:

The type of the project you just created may be WPF. In the project properties, the application tag has a Startup Object. The default value at the beginning is "Your namespace. in addition, only Not Set is available. Because the Main function is the entry of the program, VS will search for which objects can be used as Startup Objects Based on the Main function. But when we look at the code of the App class, we will find that there is no Main function in it by default. What is the problem? It turns out that VS will automatically complete the code in combination with xaml during compilation. X: Class = "Your namespace. App" in xaml, where "Your namespace. App" will appear as an optional Startup Object.
When the Main function is added to another class, this class will also be listed in the optional list of Startup objects (sometimes you may need to restart VS to see new items in Startup Object ). For the above example (the file name is Startup. cs), "namespace. Startup" is used as Startup Object.

 

Summary

The preceding implementation is a real single instance, instead of simply checking whether the program has been started by finding the same process name. It is unreliable to view single instance by using the same process name. Imagine: the hacker does not do anything, just to occupy the process name wherever it is. If you use the same process name to view single partitions. Of course, if this malicious program executes: regularly check whether this process exists, and if yes, immediately kill it, it is more troublesome.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.