当前位置: 首页 > news >正文

C# winform判断自身程序是否已运行,如果已运行则激活窗体

C# winform判断自身程序是否已运行,如果已运行则激活窗体

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{internal static class Program{[DllImport("user32.dll")]public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){Process currentProcess = Process.GetCurrentProcess();Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);if (processes.Length > 1){//MessageBox.Show("此软件已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);IntPtr handle = GetOldProcess(processes, currentProcess).MainWindowHandle;SwitchToThisWindow(handle, true);    // 激活,显示在最前  Thread.Sleep(1000);Environment.Exit(1);}Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}/// <summary>/// 获取老的进程/// </summary>/// <param name="processes"></param>/// <returns></returns>private static Process GetOldProcess(Process[] processes, Process currentProcess){//遍历与当前进程名称相同的进程列表foreach (Process process in processes){//如果实例已经存在则忽略当前进程if (process.Id != currentProcess.Id){//返回已经存在的进程return process;}}return null;}}
}
http://www.lryc.cn/news/277052.html

相关文章:

  • 超维空间M1无人机使用说明书——21、基于opencv的人脸识别
  • Redis 持久化——AOF
  • 华为云服务介绍(二)
  • mysql列题
  • cpu缓存一致性
  • Android Framework 常见解决方案(25-1)定制CPUSET解决方案-framework部分修改
  • PyTorch 参数化深度解析:自定义、管理和优化模型参数
  • 自承载 Self-Host ASP.NET Web API 1 (C#)
  • Vue2-子传父和父传子的基本用法
  • 使用numpy处理图片——镜像翻转和旋转
  • HTML5 article标签,<time>...</time>标签和pubdate属性的运用
  • Amazing OpenAI API:把非 OpenAI 模型都按 OpenAI API 调用
  • RK3568平台开发系列讲解(驱动篇)pinctrl 函数操作集结构体讲解
  • vue购物车案例,v-model 之 lazy、number、trim,与后端交互
  • 云原生Kubernetes: Kubeadm部署K8S 1.29版本 单Master架构
  • C++协程操作
  • 计算机配件杂谈-鼠标
  • 用Python来制作一个微信聊天机器人
  • 2024年第九届机器学习技术国际会议(ICMLT 2024) 即将召开
  • 算法训练day9Leetcode232用栈实现队列225用队列实现栈
  • linux驱动(四):platform
  • Guava:Cache强大的本地缓存框架
  • #{}和${}的区别?
  • string的模拟实现
  • 算法练习:查找二维数组中的目标值
  • 考研自命题资料、考题如何找
  • MySQL 存储引擎和索引类型介绍
  • element-ui table height 属性导致界面卡死
  • Vue2.v-指令
  • Java中SpringBoot组件集成接入【Knife4j接口文档(swagger增强)】