方法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 hWnd, int 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 lpSecurityAttributes, bool bInitialOwner, string 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, true, appTitle);
            if (GetLastError() == ERROR_ALREADY_EXISTS)
            {
                MessageBox.Show("程序已经运行!");
                Application.Current.Shutdown();
            }
        }
 
    }
}
 

WPF 同一个程序 只允许 同时运行一个的更多相关文章

  1. [VC]在VC++中实现让程序只运行一个实例的方法且实现该实例

    方法一: 有时候在开发应用程序时,希望控制程序运行唯一的实例.例如,最常用的mp3播放软 件Winamp,由于它需要独占计算机中的音频设备,因此该程序只允许自身运行唯一的一个例程.在Visual C+ ...

  2. Qt之运行一个实例进程

    简述 发布程序的时候,我们往往会遇到这种情况: 只需要用户运行一个实例进程 用户可以同时运行多个实例进程 一个实例进程的软件有很多,例如:360.酷狗- 多个实例进程的软件也很多,例如:Visual ...

  3. WPF 之 WPF应用程序事件

    当新建一个wpf应用程序,会自动生成一个App.xaml和MainWindow.xaml文件. 其中 App.xam 用来设置Application,应用程序的起始文件和资源及应用程序的一些属性和事件 ...

  4. Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作

    Oracle 远程访问配置   服务端配置 如果不想自己写,可以通过 Net Manager 来配置. 以下配置文件中的 localhost 改为 ip 地址,否则,远程不能访问. 1.网络监听配置 ...

  5. VC 实现程序只运行一个实例,并激活已运行的程序

    转载:http://blog.sina.com.cn/s/blog_4b44e1c00100bh69.html 进程的互斥运行:CreateMutex函数实现只运行一个程序实例 正常情况下,一个进程的 ...

  6. VC只运行一个程序实例

    方法有很多,以下只是提供一种用的多的 一. 单文档程序 在程序App类的InitInstance中添加如下代码 BOOL CDDZApp::InitInstance() { /*只运行一个实例*/ / ...

  7. C# JabLib系列之如何保证只运行一个应用程序的实现

    保证只运行一个应用程序的C#实现: using System;using System.Collections.Generic;using System.Linq;using System.Windo ...

  8. 如何让Windows程序只运行一个程序实例?

    要实现VC++或者MFC只运行一个程序实例,一般采用互斥量来实现,即首先用互斥量封装一个只运行一个程序实例的函数接口: HANDLE hMutex = NULL; void MainDlg::RunS ...

  9. wpf只运行一个实例

    原文:wpf只运行一个实例 在winform下,只运行一个实例只需这样就可以: 1. 首先要添加如下的namespace: using System.Threading; 2. 修改系统Main函数, ...

随机推荐

  1. Codeforces Round #283 (Div. 2) C. Removing Columns 暴力

    C. Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. MVC的Action上下文:ActionExecutingContext

    就上图来看,大家注意了吗,ActionExecutingContext对象一共有3处引用.下面我来一一解析: 调用base.OnActionExecuting(filterContext)这个后,才会 ...

  3. nginx新建nginx_fzjh.conf文件,不使用默认配置文件

    worker_processes 4; events{ worker_connections 1024; } http{ server { listen 80; server_name myserve ...

  4. Install WordPress Plugins without FTP Access

    WordPress will only prompt you for your FTP connection information while trying to install plugins o ...

  5. 解决Xilinx ISE在Win8下打开崩溃闪退的方法

    http://www.121down.com/article/article_13651.html 坑爹的ISE对win8无法完美支持(包括目前最新的14.6),在使用64位ISE时点击OPEN之类的 ...

  6. java基础学习总结——网络编程

    一.网络基础概念 首先理清一个概念:网络编程 != 网站编程,网络编程现在一般称为TCP/IP编程.

  7. Running multiple instances of Xamarin Studio on a Mac

    I love developing software on my MacBook Air! I got the latest version with the maximum possible spe ...

  8. ExtJs动态生成复选框

    var old_value = Ext.get("fgzr_select").getValue() if(old_value == ""){ document. ...

  9. python测试开发django-6.模板中include使用

    前言 当我们打开一个网站的时候,在打开不同的页面时候,会发现每个页面的顶部.底部内容都差不多,这样就可以把这些公共的部分,单独抽出来. 类似于python里面的函数,把公共部分写成函数,然后调用就行了 ...

  10. CASpringAnimation的使用

    CASpringAnimation的使用 效果 源码 https://github.com/YouXianMing/Animations // // CASpringAnimationControll ...