WPF-单实例运行设置
来源:互联网 发布:软件开发前期准备工作 编辑:程序博客网 时间:2024/04/30 11:39
虽然上面我们简单介绍了WPF应用的生命周期,但是默认情况下我们可以打开一个应用程序多个实例,例如你双击一个exe多次。当然有些时候这么做会带来很多好处,但是有时我们又不希望这么做,要避免这个问题其实很简单,同WinForm中单实例运行一个应用是一样的,我们只需要在应用程序启动时创建一个”排他锁”,修改App.xaml.cs如下:
using System; using System.Windows; using System.Threading;namespace WPFLifeCycle { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { Mutex mutex=null; protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); bool createdNew = false; mutex = new Mutex(true, "WPFLifeCycle",out createdNew); if (!createdNew) { MessageBox.Show("程序正在运行中,无法启动另一个实例!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning); this.Shutdown(); } } } }
此时如果我们已经运行了WPFLifeCycle.exe,当再双击此应用则会给出提示:
0 0
- WPF-单实例运行设置
- WPF实现单例运行 - 唯一实例运行
- c# wpf单实例运行并激活前实例
- WPF单实例
- WPF 单实例方法
- WPF单实例限制
- wpf只运行一个实例
- wpf只运行一个实例
- wpf只运行一个实例
- 单实例运行
- VB 单实例运行
- 单实例运行程序
- 实现单实例运行
- WinForm单实例运行
- C# 单实例运行
- c# 单实例运行
- Winform 单实例运行
- C++ 单实例运行
- reason: 'UIPopoverPresentationController (<UIPopoverPresentationController: 0x7f223f40>)。。。
- 玩转Android Camera开发(三):国内首发---使用GLSurfaceView预览Camera 基础拍照demo
- ora-01653表空间大小不足,以验证成功
- 定时 监控 shell 服务宕机自动重启,并发送短信通知
- vs2008 + qt4.8(both in win7 32bit and 64bit)
- WPF-单实例运行设置
- 使用 django channels 作为邮件发送队列
- 将Unity的脚本封装为dll文件(使用monodevelop编译器)
- Shadow Map原理和改进
- 关于win 10系统无法保存运行命令的解决方法
- 使用nprobe抓取网络数据包存到本地
- 玩转Android Camera开发(四):预览界面四周暗中间亮,只拍摄矩形区域图片(附完整源码)
- 关于retrofit的Post请求
- Windows系统 MongoDB 各个64位版本下载地址