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. ocvate常用函数

    1.生成矩阵相关 https://www.coursera.org/learn/machine-learning/lecture/9fHfl/basic-operations 1. 初始化矩阵 a = ...

  2. CF1151F Sonya and Informatics (计数dp+矩阵优化)

    题目地址 Solution (duyi是我们的红太阳) (这里说一句:这题看上去是一个概率dp,鉴于这题的概率dp写法看上去不好写,我们其实可以写一个计数dp) 首先拿到这个题目我们要能设出一个普通d ...

  3. IDEA设置类和方法的注释

    类注释 #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #pa ...

  4. Spring AOP注解配置demo

    https://blog.csdn.net/yhl_jxy/article/details/78815636#commentBox

  5. 【leetcode】1110. Delete Nodes And Return Forest

    题目如下: Given the root of a binary tree, each node in the tree has a distinct value. After deleting al ...

  6. JS——数组中push对象,覆盖问题,每次都创建一个新的对象

    今天写运动框架时,发现将对象push进数组,后面的值会覆盖前面的值,最后输出的都是最后一次的值.其实这一切都是引用数据类型惹的祸.       如果你也有类似问题,可以继续看下去哦.       下面 ...

  7. Flink Forward Asia 2019 - 总结和展望(附PPT下载链接)

    11 月 28 - 30 日,北京迎来了入冬以来的第一场雪,2019 Flink Forward Asia(FFA)也在初雪的召唤下顺利拉开帷幕.尽管天气寒冷,FFA 实际到会人次超过 2000,同比 ...

  8. makefile 打印

    $(warning ----------$(abc)) $(info -----------------$(abc))

  9. [luogu]P2279 [HNOI2003]消防局的设立[贪心]

    [luogu]P2279 [HNOI2003]消防局的设立 题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两 ...

  10. ProtocolHandler继承体系