zoukankan      html  css  js  c++  java
  • WPF程序只运行一个实例

    1.WPF程序在 启动窗口的构造函数执行InitializeComponent之前判断是否已经存在实例

    • 不涉及服务器情况,可直接进行判断(不在mainwindow的构造函数中判断)
           // public MainWindow()
           // {
    // private bool _createNew; //
    string exeName = Properties.Resources.ThreadName; // Mutex mutex = new Mutex(true, exeName, out _createNew); //only one instance is allowed // if (!_createNew) // { // MessageBox.Show(Properties.Resources.ThereAlreadyApp); // App.Current.Shutdown(); // return; // } // InitializeComponent(); // }
    •  涉及服务器(好多复制粘贴的网址,不知道那个是原创,找了能找到时间最早的链接,侵删) 在 exeName字符串加 “Global\”

    如果已经有实例运行,关闭当前试图运行的程序。

    更新  --  2016-12-23,在main函数中做判断不严谨,

    托盘程序情况下,程序在桌面上显示以后,唯一ID就判断不了。改为在类App中重写OnStartup函数进行判断,强制程序退出用Environment.Exit(0);

        public partial class App : Application
        {
         public EventWaitHandle ProgramStarted { get; set; }

    protected override void OnStartup(StartupEventArgs e) { bool createNew; ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyTctiApp", out createNew); if (!createNew) { MessageBox.Show("already exit"); App.Current.Shutdown(); Environment.Exit(0); } base.OnStartup(e); } }
  • 相关阅读:
    PHP格式化时间戳函数分享
    The Mac App Store isn't working. How to fix?
    sqlite+ef+powertools
    部署node api的二三事
    node 写api几个简单的问题
    基于项目的简单的代码生成器
    h5跳转到app的实现
    几种常用的git命令
    发送post请求几种常见content-type类型
    cors(Cross-origin resource sharing)跨域资源共享
  • 原文地址:https://www.cnblogs.com/pangkang/p/5896400.html
Copyright © 2011-2022 走看看