c#中实现单件示例
来源:互联网 发布:猫咪qq群淘宝客软件 编辑:程序博客网 时间:2024/05/16 06:57
有些窗体我们不希望生成多个实例,例如Windows系统中的控制面板等,这些窗体只有一个实例,可以采用我们现在要说的单件来实现。
现在用一个单位的窗体调用来说明单件,从Form1中调用Form2,代码如下:
- Form1窗体
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace singleton
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Form frm = Form2.Singleton();
- frm.Show();
- }
- }
- }
- Form2窗体
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace singleton
- {
- public partial class Form2 : Form
- {
- private static Form2 frm = null;
- private Form2()
- {
- InitializeComponent();
- }
- public static Form2 Singleton()
- {
- if (frm == null)
- {
- frm = new Form2();
- }
- return frm;
- }
- private void Form2_FormClosing(object sender, FormClosingEventArgs e)
- {
- try
- {
- this.Dispose(true);
- frm = null;
- }
- catch
- { }
- }
- }
- }
- c#中实现单件示例
- C#实现单件
- C#的单件实现
- ISingletonDisplayer 单件模式显示器C#实现
- C++中如何实现单件模式
- 单件模式在C#中的实现--Implementing the Singleton Pattern in C#
- C#中进程单例的实现
- c#中正确实现单例
- Unity3d中C#单例模式实现
- C# 中实现单例模式
- 单件的 static 实现
- 单件模式-C++实现
- C#实现的基于单件模式的ADO.NET数据库
- 多线程安全的Singleton单件模式在C++,java与C#下的实现
- C#设计模式极简单实现(1)单件模式
- C#如何查找指定目录下文件示例
- C# 设计模式之单件模式
- c# 设计模式之单件模式
- 解决模糊查询速度慢问题(ORACLE)
- ASP验证码-GIF格式
- PPP协议链路操作的软件实现
- JAVA经典面试题
- PROFILE的管理(资源文件)
- c#中实现单件示例
- 函数参数传递
- Spring JDBC模版基本操作示例
- 做个有趣的游戏
- 理解.NET中的数据库连接池
- 在VC的非控制台程序中调用DOS界面
- RMS安装存在问题
- 动态和静态变量
- 几种上传方法介绍、比较