WPF 同一个程序 只允许 同时运行一个

 
 
 
 

方法2  当程序已经运行了 再运行这个程序时,则显示当前这个窗体  http://code.3rbang.com/cshape-run-one/
 
 
.NET 已经提供一个 类 System.Threading.Mutex 所以不采用API CreateMutex
 App.xaml.cs 中实现代码,重这写 OnStartup
 
 
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Windows;
 
namespace WpfApplication7
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        #region DllImport...
 
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
 
 
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWndint cmdShow);
 
        private const int SW_SHOW = 1;
        #endregion
 
 
 
 
        bool createdNew;
        protected override void OnStartup(StartupEventArgs e)
        {
            System.Threading.Mutex mutex = new System.Threading.Mutex(true"HelloRoman"out createdNew);
            if (!createdNew)
            {
                System.Diagnostics.Process progress1 = GetExistProcess();
                if (progress1 != null)
                {
                    ShowMainWindow(progress1);
                    Environment.Exit(0);
                }
 
            }
        }
 
 
        /// <summary>
        /// 最前端显示主窗体
        /// </summary>
        /// <param name="process"></param>
        private void ShowMainWindow(System.Diagnostics.Process process)
        {
            IntPtr mainWindowHandle1 = process.MainWindowHandle;
            if (mainWindowHandle1 != IntPtr.Zero)
            {
                ShowWindowAsync(mainWindowHandle1, SW_SHOW);
                SetForegroundWindow(mainWindowHandle1);
            }
        }
 
        /// <summary>
        /// 查看程序是否已经运行
        /// </summary>
        /// <returns></returns>
        private static System.Diagnostics.Process GetExistProcess()
        {
            System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
            foreach (System.Diagnostics.Process process1 in System.Diagnostics.Process.GetProcessesByName(currentProcess.ProcessName))
            {
                if ((process1.Id != currentProcess.Id) &&
                     (System.Reflection.Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))
                {
                    return process1;
                }
            }
            return null;
        }
 
 
    }
}

方法1
 
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
 
 
namespace WpfApplication6
{
 
 
 
 
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
 
        [System.Runtime.InteropServices.DllImport("kernel32.dll"EntryPoint = "CreateMutex")]
        public static extern IntPtr CreateMutex(int lpSecurityAttributesbool bInitialOwnerstring lpName);
 
        [System.Runtime.InteropServices.DllImport("kernel32.dll"EntryPoint = "GetLastError")]
        public static extern int GetLastError();
        private const int ERROR_ALREADY_EXISTS = 183;
 
 
 
 
        protected override void OnStartup(StartupEventArgs e)
        {
            string appTitle = "2014年5月31日7:28:29";
            IntPtr hMutex = CreateMutex(0, trueappTitle);
            if (GetLastError() == ERROR_ALREADY_EXISTS)
            {
                MessageBox.Show("程序已经运行!");
                Application.Current.Shutdown();
            }
        }
 
    }
}
 





posted @ 2014-05-31 08:16  XE2011  阅读(2501)  评论(0编辑  收藏  举报