WPF使用Mutex创建单实例程序失效
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创建单实例程序失效的更多相关文章
- 使用 WPF 创建单实例应用程序
一个简单的例子就是大家在使用很多应用程序,例如在使用Microsoft Word 时会遇到一种情况,不管你打开多少个文档,系统一次只能加载一个winword.exe 实例.当打开新文档时,文档在新窗口 ...
- WPF点滴(2) 创建单实例应用程序
最近有同事问道在应用程序启动之后,再次双击应用程序,如何保证不再启动新的应用程序,而是弹出之前已经启动的进程,本质上这就是创建一个单实例的WPF应用程序.在VS的工程树中有一个App.xaml和App ...
- WPF:如何实现单实例的应用程序(Single Instance)
原文:WPF:如何实现单实例的应用程序(Single Instance) 好吧,这是我将WPF与Windows Forms进行比较的系列文章的第四篇,讨论一下如何实现单实例(single instan ...
- C# 实现单实例程序
在我们经常使用的软件中,当我们已经打开后,再次打开时,有的软件不会出现两个.例如有道词典,会将上次的界面显示出来,或者提示我们“该程序已经运行...”.我通过一个简单的C# WPF例子来说明. 首先我 ...
- Oracle - 给rac创建单实例dg,并做主从切换
一.概述 本文将介绍如何给rac搭建单节点的dg,以及如何对其进行角色转换.预先具备的知识(rac搭建,单实例-单实例dg搭建) 二.实验环境介绍 主库rac(已安装rac,并已有数据库orcl)ra ...
- 关于struts和Spring 结合到一起之后存在ACtion创建单实例还是多
struts 2的Action是多实例的并非单例,也就是每次请求产生一个Action的对象.原因是:struts 2的Action中包含数据,例如你在页面填写的数据就会包含在Action的成员变量里面 ...
- WPF学习笔记 - 如何用WPF创建单实例应用程序
使用一个已命名的(操作系统范围的)互斥量. bool mutexIsNew; using(System.Threading.Mutex m = new System.Threading.Mulex(t ...
- WPF简介:VS创建桌面应用程序
1.简介 1/ 什么是WPF WPF,Windows Presentation Foundation也,译过来就是"Windows呈现基础",你看它的目的非常明确,就是用来把数据& ...
- [WPF]WPF设置单实例启动
WPF设置单实例启动 使用Mutex设置单实例启动 using System; using System.Threading; using System.Windows; namespace Test ...
随机推荐
- oracle 主键自增并获取自增id
1 创建表 /*第一步:创建表格*/ create table t_user( id int primary key, --主键,自增长 username varchar(20), password ...
- GNU ARM eclipse 安装--Linux 版本
官方网站:https://gnu-mcu-eclipse.github.io/ 1. 工具链安装 1.1 xpm 安装 安装 nodejs: sudo apt-get install nodejs s ...
- Springboot配置文件占位符
一.配置文件占位符 1.application.properties server.port=8088 debug=false product.id=ID:${random.uuid} product ...
- mysql 数据库url
jdbc:mysql://localhost:3306/database?useUnicode=true&useJDBCCompliantTimezoneShift=true&useL ...
- flask_session
flask_session和Flask中的session相比,比较简单,省去了 secret_key 首先,导入flask_session 模块 from flask_session import ...
- 举个例子去理解vuex(状态管理),通俗理解vuex原理,通过vue例子类比
通俗理解vuex原理---通过vue例子类比 本文主要通过简单的理解来解释下vuex的基本流程,而这也是vuex难点之一. 首先我们先了解下vuex的作用vuex其实是集中的数据管理仓库,相当于数 ...
- 七牛云对象存储kodo使用体验
在这里,我使用了七牛云的对象存储Kodo,和阿里云的OSS,还有腾讯云的COS是同样的产品 oss相关术语 包依赖关系解决 unrecognized import path "golang. ...
- python魔法属性
1.__doc__:表示类的描述信息 class Person(object): '''定义人的类''' def func(self): pass print(Person.__doc__) 结果为: ...
- HTML canvas中translate()与rotate()的理解
首先,当我们在页面上初始化canvas时,相当于在上面放了一块画布,这块画布我们可以理解为上面有一个坐标系(如下图),左上角是原点,往右是X轴的正方向,往下是Y轴的正方向,我们在画布上绘制的内容都是基 ...
- css简单实现带箭头的边框
原文地址 https://tianshengjie.cn/artic... css简单实现带箭头的边框 普通边框 <style> .border { width: 100px; heigh ...