vs2019

1.引入名称空间

using System.Threading;

using System.Runtime.InteropServices;

2.导入dll并声明方法

[DllImport("User32", CharSet = CharSet.Unicode)]

static extern IntPtr FindWindowW(string lpClassName, string lpWindowName);

[DllImport("User32", CharSet = CharSet.Unicode)]

static extern Boolean SetForegroundWindow(IntPtr hWnd);

3 修改后App.xaml.cs如下

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows; namespace WpfApp1
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application { protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Mutex mutex = new Mutex(true, "单实例", out bool isNewInstance);//“单实例”是MainWindow.xaml中的Title
if (isNewInstance != true)
{ IntPtr intPtr = FindWindowW(null, "单实例");
if (intPtr != IntPtr.Zero)
{
SetForegroundWindow(intPtr);
}
Shutdown();
}
} [DllImport("User32", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowW(string lpClassName, string lpWindowName); [DllImport("User32", CharSet = CharSet.Unicode)]
static extern Boolean SetForegroundWindow(IntPtr hWnd);
} }

运行生成后的exe程序发现没有实现单实例的效果。

4重新修改App.xaml.cs文件

前面是将mutex声明为局部变量

Mutex mutex = new Mutex(true, "单实例", out bool isNewInstance);//“单实例”是MainWindow.xaml中的Title

这次将mutex声明为字段

...
Mutex mutex;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
mutex = new Mutex(true, "单实例", out bool isNewInstance);
if (isNewInstance != true)
{ IntPtr intPtr = FindWindowW(null, "单实例");
if (intPtr != IntPtr.Zero)
{
SetForegroundWindow(intPtr);
}
Shutdown();
}
}
...

重新生成后实现效果

WPF使用Mutex创建单实例程序失效的更多相关文章

  1. 使用 WPF 创建单实例应用程序

    一个简单的例子就是大家在使用很多应用程序,例如在使用Microsoft Word 时会遇到一种情况,不管你打开多少个文档,系统一次只能加载一个winword.exe 实例.当打开新文档时,文档在新窗口 ...

  2. WPF点滴(2) 创建单实例应用程序

    最近有同事问道在应用程序启动之后,再次双击应用程序,如何保证不再启动新的应用程序,而是弹出之前已经启动的进程,本质上这就是创建一个单实例的WPF应用程序.在VS的工程树中有一个App.xaml和App ...

  3. WPF:如何实现单实例的应用程序(Single Instance)

    原文:WPF:如何实现单实例的应用程序(Single Instance) 好吧,这是我将WPF与Windows Forms进行比较的系列文章的第四篇,讨论一下如何实现单实例(single instan ...

  4. C# 实现单实例程序

    在我们经常使用的软件中,当我们已经打开后,再次打开时,有的软件不会出现两个.例如有道词典,会将上次的界面显示出来,或者提示我们“该程序已经运行...”.我通过一个简单的C# WPF例子来说明. 首先我 ...

  5. Oracle - 给rac创建单实例dg,并做主从切换

    一.概述 本文将介绍如何给rac搭建单节点的dg,以及如何对其进行角色转换.预先具备的知识(rac搭建,单实例-单实例dg搭建) 二.实验环境介绍 主库rac(已安装rac,并已有数据库orcl)ra ...

  6. 关于struts和Spring 结合到一起之后存在ACtion创建单实例还是多

    struts 2的Action是多实例的并非单例,也就是每次请求产生一个Action的对象.原因是:struts 2的Action中包含数据,例如你在页面填写的数据就会包含在Action的成员变量里面 ...

  7. WPF学习笔记 - 如何用WPF创建单实例应用程序

    使用一个已命名的(操作系统范围的)互斥量. bool mutexIsNew; using(System.Threading.Mutex m = new System.Threading.Mulex(t ...

  8. WPF简介:VS创建桌面应用程序

    1.简介 1/ 什么是WPF WPF,Windows Presentation Foundation也,译过来就是"Windows呈现基础",你看它的目的非常明确,就是用来把数据& ...

  9. [WPF]WPF设置单实例启动

    WPF设置单实例启动 使用Mutex设置单实例启动 using System; using System.Threading; using System.Windows; namespace Test ...

随机推荐

  1. EffectiveC++条款04:确定对象被使用前已先被初始化

    不要混淆赋值和初始化,对于大多数类型而言,比起先调用默认构造函数然后调用赋值操作符,只调用一次拷贝构造函数是高效的 对于内置类型,也需要成员初值列(member initialization list ...

  2. php内置函数分析之array_diff_assoc()

    static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_type) /* {{{ */ { uint ...

  3. POI2012 ODL-Distance

    链接P3532 [POI2012]ODL-Distance 设\(f_{i,j}\)表示他给定的函数,\(g_i\)表示\(i\)的质因数个数 那么\[f_{i,j}=g_{\frac {i*j}{g ...

  4. Node.js企业开发:应用场景

    要想用Node.js首先需要知道它到底是什么, 有哪些优缺点. 然后我们才能知道到底 Node.js 适合哪些应用场景. Node.js 维基百科:“Node.js 是谷歌 V8 引擎.libuv平台 ...

  5. 用vue构建项目同一局域网下通过ip访问

    在webpack配置文件下改为 host:'0.0.0.0' 改为后启动跳转不到登录页面 需手动修改浏览器上的0.0.0.0:8080为自己ip加上:8080 就可以在别的电脑上进行访问了 举一反三: ...

  6. 【java工具类】AES加密解密

    百度百科一下,AES:高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准 ...

  7. HDU 6582 Path

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  8. JS onclick中this用法

    当在dom元素中使用onclick绑定事件的时候,可以使用this来指向该元素对象. 打印输出的内容为: 所以可以通过该this对象来获取子元素 //通过element获取该对象下的一个audio标签 ...

  9. nginx之Geoip读取地域信息模块

    1 geoip_module模块 基于IP地址匹配MaxMind GeolP二进制文件,读取IP所在地域信息. yum install nginx-module-geoip geoip2已经有了,安装 ...

  10. router-link传递参数并获取

    跳转链接: <router-link :to="{path:'libraryDetail/', query:{library_id:data.library_id}}"> ...