博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf只运行一个实例
阅读量:7188 次
发布时间:2019-06-29

本文共 1885 字,大约阅读时间需要 6 分钟。

原文:

在winform下,只运行一个实例只需这样就可以:

1. 首先要添加如下的namespace:

using System.Threading;

2. 修改系统Main函数,大致如下:

        bool bCreatedNew;      

        //Create a new mutex using specific mutex name

        Mutex m =new Mutex( false, "myUniqueName", out bCreatedNew );

        if( bCreatedNew )

            Application.Run(new yourFormName());

在wpf中如何做呢?

我在微软论坛发帖之后,有高手提出了如下的解决办法,我试验之后,此法可行,就贴出来和大家分享。有人还有其他解决办法,可以写出来一起探讨:

首先 引用Microsoft.VisualBasic 然后新建一个类 single public  class single:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase     {
App a; public single() {
this.IsSingleInstance = true; } protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs) {
a = new App(); a.InitializeComponent(); a.Run(); return false; } protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs) {
base.OnStartupNextInstance(eventArgs); a.activate(); } } app.cs public partial class App : Application {
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e); Window1 w = new Window1(); w.Show(); } public void activate() {
MainWindow.Activate(); } private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e) {
} private void Application_Startup(object sender, StartupEventArgs e) {
} private void Application_Exit(object sender, ExitEventArgs e) {
} } app.g.cs [System.STAThreadAttribute()] [System.Diagnostics.DebuggerNonUserCodeAttribute()] public static void Main(string [] a ) {
single s = new single(); s.Run(a); }
刚开始运行的时候,虽然实现了再次点击不会有新实例,但是却每次都实例了两个窗口的情况。去掉 app.xaml中StartupUri属性就OK了。

转载地址:http://ukfkm.baihongyu.com/

你可能感兴趣的文章
HTML 5 Canvas
查看>>
何慕雄:对尚福林的评价
查看>>
NTP
查看>>
缓存击穿测试
查看>>
Linux菜鸟级重点
查看>>
字节排序问题
查看>>
python3读取chrome浏览器cookies
查看>>
android 网络编程 HttpGet类和HttpPost类使用详解
查看>>
数据可视化-EChart2.0使用总结1
查看>>
前后端分离下如何登录
查看>>
AJAX学习1
查看>>
effective c++条款5 了解编译器默认编写那些函数
查看>>
深入理解指针函数
查看>>
添加图片后xcode报错:resource fork, Finder information, or similar detritus not allowed
查看>>
CSS-背景渐变的兼容写法
查看>>
正则表达式--列表
查看>>
Unity3D占用内存太大的解决方法
查看>>
记一次基于Unity的Profiler性能分析
查看>>
Educational Codeforces Round 57题解
查看>>
windows10安装centos7双系统详细教程
查看>>