zoukankan      html  css  js  c++  java
  • WPF(四)Application3:如何实现单实例的应用程序(Single Instance)

    先来看第一种最简单粗暴的做法:

    检测进程名,如果名称一样,则表示程序已经启动了,就不再启动.

     protected override void OnStartup(StartupEventArgs e)
        {
            // Get Reference to the current Process
            Process thisProc = Process.GetCurrentProcess();
            // Check how many total processes have the same name as the current one
            if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)
            {
                // If ther is more than one, than it is already running.
                MessageBox.Show("Application is already running.");
                Application.Current.Shutdown();
                return;
            }
    
            base.OnStartup(e);
        }
    很简单,不是吗?但简单有什么错呢? 它很实用.
    [注意]这个代码如果在visual studio中调试则无效,因为visual studio调试用的进程是加了一个vshost的后缀的。
     
    第二种方案我觉得应该还是可以用mutex来实现嘛,看看下面的代码

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Windows;
    using System.Diagnostics;
    using System.Threading;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// App.xaml 的交互逻辑
        /// </summary>
        public partial class App : Application
        {
            protected override void OnStartup(StartupEventArgs e)
            {
                bool createNew;
                Mutex mutex = new Mutex(true, "MyApplication", out createNew);
                if (createNew)
                    base.OnStartup(e);
                else
                {
                    MessageBox.Show("程序已经启动了");
                    Application.Current.Shutdown();
                } 
            }
    
        }
    }

    看起来解决问题了,但仍然不是很理想的。最好的情况是,当用户开启第二个实例的时候,如果第一个实例没有处于活动状态,则应该激活它。

    我们很自然还是联想到了原先在Windows Forms时代的WindowsFormsApplicationBase,那里面做这个事情太简单了。

    首先,添加Microsoft.VisualBasic的引用

     

    namespace WpfApplication1
    {
        public class EntryPoint
        {
            [STAThread]
            public static void Main(string[] args)
            {
                SingleInstanceManager manager = new SingleInstanceManager();
                manager.Run(args);
            }
        }
    
        // 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
        {
            SingleInstanceApplication 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();
                return false;
            }
    
            protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
            {
                // Subsequent launches
                base.OnStartupNextInstance(eventArgs);
                app.Activate();
            }
        }
    
        public class SingleInstanceApplication : Application
        {
            protected override void OnStartup(System.Windows.StartupEventArgs e)
            {
                base.OnStartup(e);
    
                // Create and show the application's main window
                //MainWindow window = new MainWindow();
                Window1 window = new Window1();
                window.Show();
            }
    
            public void Activate()
            {
                // Reactivate application's main window
                this.MainWindow.Show();
                this.MainWindow.Activate();
            }
        }
    } 

     

     

  • 相关阅读:
    1270. 数列区间最大值(climits用法+线段树模板题)
    JDBC&DBCP总结
    1264. 动态求连续区间和(树状数组模板题)
    788. 逆序对的数量(归并排序的应用)
    归并排序(模板题)
    ZoomEye技巧
    工具或安全监测网站(不定时更新)
    bp截包
    CTF/web
    CTF/stega——图片隐写
  • 原文地址:https://www.cnblogs.com/WilliamJiang/p/2465799.html
Copyright © 2011-2022 走看看