让C#程序只运行一个实例,显示已经运行的界面
bool isNew;
mutex = new System.Threading.Mutex(true, "myproject", out isNew);
if (isNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
else
{
MessageBox.Show("本程序已经在运行!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
让程序只运行一个实例的方法二(会显示正在运行的窗口): static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
else
{
HandleRunningInstance(instance);
}
}
//返回正在运行的程序进程
public static Process RunningInstance(){
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/ ", "\\ ") == current.MainModule.FileName)
{
return process;
}
}
}
return null;//第一次运行,返回null
}
//显示正在运行的进程当前窗口
public static void HandleRunningInstance(Process instance){
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //置窗口为正常状态
SetForegroundWindow(instance.MainWindowHandle);
}
#region调用系统api
[DllImport("User32.dll ")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll ")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
#endregion
当双击某程序到它显示运行界面时,操作系统都做了啥? 假设:我们现在想要让操作系统运行「微信」,那么首先我们会怎么做嘞?在整个过程中,操作系统又做了啥? 如果这是一个面试题,你会怎么回答呢?来看看阿粉的解释~
相关文章
- load-display-image之c#版
- C#_XML与Object转换
- C#_abstract的用法
- C#报错:未将对象设置到对象实例 Object reference not set to an instance of an object
- C# StackExchange.Redis 用法总结
- C#实现Zip压缩解压实例【转】
- C# OpenCV OpenCVSharp应用实例--二值图孔洞填充方法与实现
- Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
- OpenCVSharp(C# OpenCV)图像去水印实例
- C#图像处理-OpenCVSharp教程(三十八) OpenCVSharp简单人脸识别实例
- C#图像处理-OpenCVSharp教程(三十七) OpenCVSharp简单人脸检测实例
- C#调用RabbitMQ实现消息队列
- 【C#进阶】C# 事件
- C#如何获得 WINDOWS 版本号
- C#多线程编程实例 螺纹与窗口交互
- Unity3d C#开发微信小游戏音频/音效播放问题解决过程分享
- Unity3d C#实现UGUI上箭头指示3D地图物体位置功能(含源码)
- C#手动操作DataGridView之------使用各种数据源填充表格实例
- C#特性的使用及自定义特性的建立
- ASP.NET Core(C#) 使用RSACryptoServiceProvider实现公钥和私钥加解密工具类
- .NET Core(C#)使用NPOI改变设置Excel(.xls,.xlsx)单元格背景颜色
- .NET(C#)CefSharp loadhtml(string html, string url)的使用及示例代码
- .NET(C#) 查找打印机指定打印机名称打印代码(PrintDocument)
- C# 枚举使用和对应说明获取实例
- .NET C# 调用python代码 简单实例
- C#简单工厂模式实现松耦合度四则运算
- 2019-11-29-C#-反射调用私有事件
- 2019-10-31-C#-强转空会不会出现异常