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.
查看全文
相关阅读:
1132: 零起点学算法39——多组测试数据(a+b)
1131: 零起点学算法38——求阶乘和
1130: 零起点学算法37——求阶乘
1129: 零起点学算法36——3n+1问题
1128: 零起点学算法35——再求多项式(含浮点)
1127: 零起点学算法34——继续求多项式
1126: 零起点学算法33——求多项式
Netty源码分析第1章(Netty启动流程)---->第1节: 服务端初始化
Netty源码分析(前言, 概述及目录)
基于HTK语音工具包进行孤立词识别的使用教程
原文地址:https://www.cnblogs.com/zhangqs008/p/2341101.html
最新文章
BroadcastReceiver接收系统广播消息
BroadcastReceiver的实例----基于Service的音乐播放器之二
BroadcastReceiver的实例----基于Service的音乐播放器之一
发送有序广播Ordered Broadcast
发送广播BroadcastReceiver
BroadcastReceiver的简介
使用AlarmManager定时更换壁纸----之二
USACO 1.5 Prime Palindromes
位运算基础
USACO 1.5 Number Triangles
热门文章
USACO 1.4 Mother's Milk
USACO 1.4 Arithmetic Progressions
USACO 1.3 Ski Course Design
USACO 1.3 Wormholes
NEU 1009 Happiness Hotel
最小生成树基础 (Kruskal)
NEU 1040 Count
1135: 零起点学算法42——多组测试数据(求和)IV
1134: 零起点学算法41——多组测试数据(a+b)III
1133: 零起点学算法40——多组测试数据(a+b)II
Copyright © 2011-2022 走看看