WPF之创建单实例应用程序
2013-05-11 13:43
204 查看
单实例应用程序即该应用程序只允许运行一个实例,说白了就是不能同时运行两个相同的程序。
主体框架为3个自定义类: Startup、 SimpleInstanceAppWrapper、 App
主体框架为3个自定义类: Startup、 SimpleInstanceAppWrapper、 App
// Startup.cs using System; using System.Windows; namespace WpfApplication1 { class Startup { [STAThread()] static void Main(string[] args) { SimpleInstanceAppWrapper instance = new SimpleInstanceAppWrapper(); instance.Run(args); } } }
// SimpleInstanceAppWrapper.cs // 添加引用: Microsoft.VisualBasic using System; using System.Windows; namespace WpfApplication1 { class SimpleInstanceAppWrapper : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase { public SimpleInstanceAppWrapper() { this.IsSingleInstance = true; } private App app; protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs) { app = new App(); app.Run(new MainFrame()); // MainFrame类是自定义的主窗口类 return false; } protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs) { //base.OnStartupNextInstance(eventArgs); MessageBox.Show("Cannot startup the second instance!"); } } }
// App.cs using System; using System.Windows; namespace WpfApplication1 { class App : Application { /// /// 该事件在Application.Run()方法调用之后,在主窗口显示之前发生。 /// /// 该参数包含字符串数组类型的命令行参数 protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); } /// /// 该事件在应用程序关闭时,app.Run()方法返回之前发生。 /// /// 该参数包含整数类型的退出代码 protected override void OnExit(ExitEventArgs e) { base.OnExit(e); } } }
相关文章推荐
- WPF学习笔记 - 如何用WPF创建单实例应用程序
- 使用 WPF 创建单实例应用程序
- WPF学习笔记 - 如何用WPF创建单实例应用程序
- IOS开发之小实例--创建一个简单的用于视频录制和回放的应用程序
- 图像处理控件ImageGear for .NET教程:C# WPF应用程序创建示例(2)
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
- C#学习之二---创建简单的图形应用程序-WPF
- 11、创建不使用XAML的WPF应用程序
- 利用WPF创建一个绘图应用程序
- 【WPF学习】创建WPF应用程序的三种方式
- 创建第一个基于Prism框架的WPF应用实例(含源码下载)
- WPF:如何实现单实例的应用程序(Single Instance)
- c/s结构windows桌面应用程序wpf教程-创建wpf应用
- 只使用代码创建WPF应用程序
- WPF(四)Application3:如何实现单实例的应用程序(Single Instance)
- IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
- Ext JS 6开发实例(二) :使用CMD创建应用程序
- 如何用创建命名对象来判断应用程序是否已有一个实例在运行?
- Ext JS 6开发实例(二) :使用CMD创建应用程序
- SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序