zoukankan
html css js c++ java
创建单例winform应用程序
方法1:
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Threading; using System.Reflection; static class Program { private static Mutex singleton; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); bool has=Check() ; if (has) { Form form = new Form1(); form.FormClosed += new FormClosedEventHandler(form_FormClosed); Application.Run(form); } else { MessageBox.Show("程序已启动"); } } static void form_FormClosed(object sender, FormClosedEventArgs e) { if (singleton != null) { singleton.Close(); } } private static bool Check() { bool has=false; singleton=new Mutex(false,Assembly.GetExecutingAssembly().FullName,out has); // Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName; return has; } } }
方法2:
static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Process instance = RunningInstance(); //Get the running instance. if (instance == null) { //There isn 't another instance, show our form. Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } else { Message.WriteErrMsg("程序已运行,请勿再次运行!"); } } public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); //Loop through the running processes in with the same name foreach (Process process in processes) { //忽略当前进程<strong> </strong> if (process.Id != current.Id) { //Make sure that the process is running from the exe file. if (Assembly.GetExecutingAssembly().Location.Replace("/ ", "\\ ") == current.MainModule.FileName) { //Return the other process instance. return process; } } } //No other instance was found, return null. return null; } }
方法1:不能避免多用户时的情况,多用户登陆系统时,还是可以开启多个实例的。
方法2:检查系统进程,可以解决多用户的问题,推荐方法2.
查看全文
相关阅读:
“12306”的架构到底有多牛逼
数字治理
浅谈web网站架构演变过程
MapReduce基本原理
Flink+Hologres亿级用户实时UV精确去重最佳实践
全链路压测体系建设方案的思考与实践
如何做好一场技术演讲?
“控本焦虑”的工程企业 用钉钉宜搭找到了低成本数字化的“捷径”
友盟+《小程序用户增长白皮书》:从五个角度入手分析小程序数据
数字化让618有了洞悉消费者内心的“大脑”
原文地址:https://www.cnblogs.com/zhangqs008/p/2341101.html
最新文章
Qt 多pro与多pri合作编程
Qt 项目pri使用浅谈
C# 关于路径的总结
C# @的用法总结
C# | Winform编程控件之数字输入框控件(numericUpDown)
C# WinForm程序怎么打包成安装项目(VS2010图解)
C# 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记
C# 异常:在调用OLE之前,必须将当前线程设置为单线程单单元(STA)模式
VisionPro 关闭程序断开相机连接
VisionPro 脚本
热门文章
VsionPro 相机操作类
VisionPro 图像操作
VisionPro 相机硬件触发笔记
VisionPro 相机取像操作,.NET4.0
VisionPro 相机操作学习
Qt QPainter的使用及矩形、圆形等常见图形的画法
什么是B端产品
图解商业模式实例
数据仓库建模方法与模型
服务器领域十大技术趋势
Copyright © 2011-2022 走看看