C#中程序的互斥运行
2007-04-08 22:44
253 查看
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Threading;
namespace exam_使用程序只能够运行一个
{
pulic class Forms:System.Windows.Forms.Form
{
[STAThread]
static void Main()
{
bool createdNew;
Mutex m=new Mutext(true,”test”,out createdNew);
if (createdNew)
{
Application.Run(new Form1());
m.ReleaseMutex();
}
else
{
Messagebox.Show(“本程序只允许同时运行一个”);
}
}
}
}
程序通过Mutex m=new Mutext(true,”test”,out createdNew);语句创建一个互斥体变量m,其中true参数表示调用线程拥有互斥体的初始所属权,test为互斥体名,并且将调用线程是否已被授权互斥体的初始所属权的布尔值保存在createdNew变量中。然后通过判断该变量值决定是否启动本程序,如果为true,则无正在运行的本实例,通过Application.Run(new Form1())语句启动程序;否则显示一个对话框并结束程序运行。
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Threading;
namespace exam_使用程序只能够运行一个
{
pulic class Forms:System.Windows.Forms.Form
{
[STAThread]
static void Main()
{
bool createdNew;
Mutex m=new Mutext(true,”test”,out createdNew);
if (createdNew)
{
Application.Run(new Form1());
m.ReleaseMutex();
}
else
{
Messagebox.Show(“本程序只允许同时运行一个”);
}
}
}
}
程序通过Mutex m=new Mutext(true,”test”,out createdNew);语句创建一个互斥体变量m,其中true参数表示调用线程拥有互斥体的初始所属权,test为互斥体名,并且将调用线程是否已被授权互斥体的初始所属权的布尔值保存在createdNew变量中。然后通过判断该变量值决定是否启动本程序,如果为true,则无正在运行的本实例,通过Application.Run(new Form1())语句启动程序;否则显示一个对话框并结束程序运行。
相关文章推荐
- c#实现程序的互斥运行
- C# 创建互斥程序_只允许运行一个程序实例
- c#实现程序的互斥运行
- C# 创建互斥程序_只允许运行一个程序实例
- c# 更改注册表开机启动+mutex(互斥,同一时间同一台电脑只能运行一个程序)
- C# 创建互斥进程(程序只能运行一个实例)
- C#程序双击运行之后,界面不显示,但是在任务管理器有进程(一个winform找bug之旅)
- C#:程序运行时间间隔
- C#实现测量程序运行时间及cpu使用时间
- C#打包程序在WIn8.1系统运行失败
- 使用Mutex实现程序单实例运行(c#)
- C# 关闭登录窗体,进入主窗体后,主窗体关闭,程序进程仍在运行的问题
- C# 开机检测是否运行某程序的方法
- 将多个C#控制台程序整合到一个控制台程序中运行
- 使用Mutex实现程序单实例运行(c#)
- c#计算程序的运行时间
- 【转】C#获取当前程序运行路径的方法集合
- 如何配置C#环境变量并运行第一个程序
- c# 运行大运算程序主窗体卡掉的解决
- c#winform程序运行时出现“无法在证书存储区中找到清单签名证书”错误的解决方法