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.
查看全文
相关阅读:
Vi 和 Vim
The C Programming Language-Chapter 5 Pointers and Arrays
C# 4.0开始,泛型接口和泛型委托都支持协变和逆变
数据库中的锁 and java StampedLock ReadWriteLock
NetCore and ElasticSearch 7.5
网关项目 ReverseProxy
异常捕获&打印异常信息
刷新:重新发现.NET与未来
2019 中国.NET 开发者峰会正式启动
.NET开发者必须学习.NET Core
原文地址:https://www.cnblogs.com/zhangqs008/p/2341101.html
最新文章
baguetteBox.js
可以作为你的候选的12个很赞的移动开发框架
Baffle.js – 用于实现文本模糊效果的 JavaScript 库
前端工程师和设计师必读文章推荐【系列三十七】
Prezento – 轻量、简单的 jQuery 幻灯片插件
7种方法实现移动端Retina屏幕1px边框效果
2017年最新20个轻量的 JavaScript 库和插件
Vue Admin
前端工程师和设计师必读文章推荐【系列三十六】
Scrollanim – CSS3 & JavaScript 创建滚动动画
热门文章
Hero Patterns
axios
Bulma
PixiJS
网络协议及tcp协议详解(超清楚的大图,难得还解释了会话层和表示层。服务端有一个保活计时器,时间通常是设置为2小时。发送一个探测报文段,以后每隔75秒钟发送一次,一连发送10个探测报文)
李重俊和李多祚果断发动攻击的话,或许还有几分胜算,可惜的是,两人犹豫不定错失良机(早就没有退路了啊,想被原谅都是难上加难,更别说免死都不太可能。完全没有李世民的果断。另外应该兵分两路,不给中宗时间。其实李世民就是这么干的,何况对付武三思只需要极少量兵力,控制皇帝也比对付武三思更重要)
真搞热战,谁碾压谁还不一定那——发电量等于整个西方之和——云舰队+充电战舰
分工的重要性:不专习一种特殊业务,那么他们不论是谁,绝对不能一日制造二十枚针,说不定一天连一枚也制造不出
Explain 执行计划MYSQL优化
Docker Jenkins Gitlab CI/CD
Copyright © 2011-2022 走看看