您的位置:首页 > 其它

一台电脑只能启动一个客户端。——Mutex 类的简单运用

2012-02-28 15:29 375 查看
C#——WinForm程序

一台电脑只能启动一个客户端。——Mutex 类的简单运用

static class Program

{

public static System.Threading.Mutex Run;

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main()

{

bool noRun = false;

Run = new System.Threading.Mutex(true, "SSHQ", out noRun);

if (noRun)

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new frmSshqSm());

}

else

{

MessageBox.Show("程序已经运行!", "提示");

}

}

}

基本概述  Mutex:/mjuteks/

  互斥(体) 又称同步基元使用了System.Threading 命名空间。

  当创建一个应用程序类时,将同时创建一个系统范围内的命名的Mutex对象。这个互斥元在整个操作系统中都是可见的。当已经存在一个同名的互斥元时,构造函数将会输出一个布尔值。程序代码通过检测这个布尔值来判断指定的程序实例是否启动,如果已经存在同名互斥元的话,则显示一个对话框,告知用户应用程序已经启动,并退出应用程序。

  A data structure for mutual exclusion, also known as a binary semaphore. A mutex is basically just a multitasking-aware binary flag that can be used to synchronize the activities of multiple tasks. As such, it can be used to protect critical sections of
the code from interruption and shared resources from simultaneous use.

  表现互斥现象的数据结构,也被当作二元信号灯。一个互斥基本上是一个多任务敏感的二元信号,它能用作同步多任务的行为,它常用作保护从中断来的临界段代码并且在共享同步使用的资源。

详细资料看百度:http://baike.baidu.com/view/1889552.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐