Asp.net Winform Singleton mode and introduction

Source: Internet
Author: User
Tags garbage collection static class

What is Singleton mode?
As the object creation mode, the singleton mode ensures that a class has only one instance and is self-instantiated (that is, private structure) and provides

Instances. This is the singleton mode. This class is called a singleton class.

Features of Singleton mode:
1. Only one instance can be created in Singleton mode;
2. The Singleton mode must have a private constructor;
3. In Singleton mode, a global access point must be provided for the entire system.

Note:
1. The constructor in Singleton mode can be of the protected type to allow subclass derivation, but it cannot be public;
2. The Singleton mode generally does not require the support of the icloneable interface. The Singleton mode generally does not support serialization, because this will lead to object instances and Singleton

The original intention of the model is contrary;
3. In Singleton mode, only object creation management is taken into account, and object destruction management is not considered. For platforms that support garbage collection, we generally do not

It is necessary to perform special management on its destruction.
4. Unable to cope with multi-threaded environments: in a multi-threaded environment, using the Singleton mode may still obtain multiple instance objects of the singleton class.

Typical case of Singleton mode (c #)
The relationship between the mdiparent and the child form in winform should be a typical Singleton mode.
First, create a windows application in visual studio, create two forms from1 and form2, and set form1 as the parent form, form2

 

 

   
Using system;
Using system. threading;
Using system. windows. forms;

Static class program
{
Public static eventwaithandle programstarted;

[Stathread]
Static void main ()
{
// Try to create a naming event
Bool createnew;
Programstarted = new eventwaithandle (false, eventresetmode. autoreset, "mystartevent", out createnew );

// If the name event already exists (the previous running instance exists), send an event notification and exit
If (! Createnew)
{
Programstarted. set ();
Return;
}

Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Application. run (new form1 ());
}
}



C # code

// Form1.cs
//
Using system;
Using system. windows. forms;
Using system. threading;

Public partial class form1: form
{
Policyicon policyicon1 = new policyicon ();

Public form1 ()
{
// Initializecomponent ();
This. Policyicon1.text = "double click me to show window ";
This. Policyicon1.icon = system. drawing. systemicons. information;
This. Policyicon1.doubleclick + = onpolicyicondoubleclicked;
This. sizechanged + = onsizechanged;
Threadpool. registerwaitforsingleobject (program. programstarted, onprogramstarted, null,-1, false );
}

// When minimized, place it on the system tray.
Void onsizechanged (object sender, eventargs e)
{
If (this. windowstate = formwindowstate. minimized)
{
This. Policyicon1.visible = true;
This. visible = false;
}
}

// The resume window is displayed when you double-click the tray icon.
Void onpolicyicondoubleclicked (object sender, eventargs e)
{
This. visible = true;
This. Policyicon1.visible = false;
This. windowstate = formwindowstate. normal;
}

// When receiving a notification from the second process, a balloon message is displayed.
Void onprogramstarted (object state, bool timeout)
{
This. Policyicon1.showballoontip (2000, "hello", "I am here...", tooltipicon.info );
}
}

Related Article

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.