C#应用程序实现单例模式
2013-12-26 11:24
288 查看
该文档引自网址http://wenwen.soso.com/z/q85588071.htm
限制启动一个应用程序窗口,再启动,将把第一个启动的窗口从任务栏里还原出来。
代码如下:
限制启动一个应用程序窗口,再启动,将把第一个启动的窗口从任务栏里还原出来。
代码如下:
using Microsoft.VisualBasic.ApplicationServices; static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); SingleInstanceManager manager = new SingleInstanceManager();//单实例管理器 manager.Run(new string[]{}); //Application.Run(new frmMain()); //屏蔽掉了以前的加载头 } // Using VB bits to detect single instances and process accordingly: // * OnStartup is fired when the first instance loads // * OnStartupNextInstance is fired when the application is re-run again // NOTE: it is redirected to this instance thanks to IsSingleInstance public class SingleInstanceManager : WindowsFormsApplicationBase { frmMain app; public SingleInstanceManager() { this.IsSingleInstance = true; } protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e) { // First time app is launched //app = new SingleInstanceApplication(); //app.Run(); app = new frmMain();//改为自己的程序运行窗体 Application.Run(app); return false; } protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) { // Subsequent launches base.OnStartupNextInstance(eventArgs); app.Activate(); } } }
相关文章推荐
- .NET平台自带的AOP机制 转自《设计模式--基于c#的工程化实现及扩展》
- C#回顾 - 7.如何使用反射实现工厂模式?
- 二十三种设计模式之原型模式的C#实现
- C#原型模式之深复制实现
- 《模式——工程化实现及扩展》(设计模式C# 版)《适配器模式 Adapter》——“自我检验"
- C#实现托盘应用程序
- C# 单例模式Lazy<T>实现版本
- 利用C#实现标准的Dispose模式
- C#基础系列——委托实现简单设计模式
- C#实现Web应用程序定时启动任务
- C#的单例模式实现
- C#实现重启应用程序
- C# window Service实现调用有UI的应用程序(关于win xp以后的window系统)
- Composite模式C#实现
- C#中标准Dispose模式的实现
- 【转载】多线程--C#利用多线程实现消费者和生产者模式
- 设计模式之(策略模式)(C#实现)(程杰)
- 【设计模式】观察者模式(observer)c#/python实现
- 发布订阅者模式之C#委托实现
- C# 反射工厂模式的实现