WPF:如何實現單實例的應用程序(Single Instance)

 

先來看第一種最簡單粗暴的作法:html

檢測進程名,若是名稱同樣,則表示程序已經啓動了,就再也不啓動.app

    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();
            } 
        }

    }
}

這一種作法的結果與第一種很相似,或者說沒有任何區別。ide

 

看起來解決問題了,但仍然不是很理想的。最好的狀況是,當用戶開啓第二個實例的時候,若是第一個實例沒有處於活動狀態,則應該激活它。this

咱們很天然仍是聯想到了原先在Windows Forms時代的WindowsFormsApplicationBase,那裏面作這個事情太簡單了。spa

首先,添加Microsoft.VisualBasic的引用3d

image

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();
        }
    }
} 轉:http://www.cnblogs.com/chenxizhang/archive/2010/03/25/1694605.html
相關文章
相關標籤/搜索