您可以在这里快速查找:


 
您的位置: 编程学习 > asp.net教程 > 200510
文章分类

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02

Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

VB编程
2006: 02

Asp编程
2005: 11 12
2006: 01 02

C++/VC
2005: 10 11 12
2006: 01 02

Delphi
2005: 12
2006: 01 02

其它

 本文章适合所有读者

C#中程序的互斥运行

OAMVP

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())语句启动程序;否则显示一个对话框并结束程序运行。