CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

请教Mutex的用法

楼主happyie(hanke)2005-11-01 14:38:54 在 .NET技术 / C# 提问

同上 问题点数:100、回复次数:2Top

1 楼jinjazz(近身剪)回复于 2005-11-01 14:46:01 得分 50

[StructLayout(   LayoutKind.Sequential)]  
  public   class   SECURITY_ATTRIBUTES    
  {  
  public   int   nLength;    
  public   int   lpSecurityDescriptor;    
  public   int   bInheritHandle;    
  }  
   
  [System.Runtime.InteropServices.DllImport("kernel32")]  
  private   static   extern   int   GetLastError();  
  [System.Runtime.InteropServices.DllImport("kernel32")]  
  private   static   extern   IntPtr   CreateMutex(SECURITY_ATTRIBUTES   lpMutexAttributes,bool   bInitialOwner,string   lpName);  
  [System.Runtime.InteropServices.DllImport("kernel32")]  
  private   static   extern   int   ReleaseMutex(IntPtr   hMutex);  
  const   int   ERROR_ALREADY_EXISTS   =   0183;  
   
  [STAThread]  
  static   void   Main()    
  {  
  IntPtr   hMutex;  
  hMutex=CreateMutex(null,false,"test");  
  if   (GetLastError()!=ERROR_ALREADY_EXISTS)  
  {  
  Application.Run(new   Form1());  
  }  
  else  
  {  
  MessageBox.Show("本程序只允许同时运行一个");  
  ReleaseMutex(hMutex);  
  }  
  }Top

2 楼pupo(泡泡)回复于 2005-11-01 14:46:02 得分 50

using   System;  
  using   System.Threading;  
   
  class   Test  
  {  
          //   Create   a   new   Mutex.   The   creating   thread   does   not   own   the  
          //   Mutex.  
          private   static   Mutex   mut   =   new   Mutex();  
          private   const   int   numIterations   =   1;  
          private   const   int   numThreads   =   3;  
   
          static   void   Main()  
          {  
                  //   Create   the   threads   that   will   use   the   protected   resource.  
                  for(int   i   =   0;   i   <   numThreads;   i++)  
                  {  
                          Thread   myThread   =   new   Thread(new   ThreadStart(MyThreadProc));  
                          myThread.Name   =   String.Format("Thread{0}",   i   +   1);  
                          myThread.Start();  
                  }  
   
                  //   The   main   thread   exits,   but   the   application   continues   to  
                  //   run   until   all   foreground   threads   have   exited.  
          }  
   
          private   static   void   MyThreadProc()  
          {  
                  for(int   i   =   0;   i   <   numIterations;   i++)  
                  {  
                          UseResource();  
                  }  
          }  
   
          //   This   method   represents   a   resource   that   must   be   synchronized  
          //   so   that   only   one   thread   at   a   time   can   enter.  
          private   static   void   UseResource()  
          {  
                  //   Wait   until   it   is   safe   to   enter.  
                  mut.WaitOne();  
   
                  Console.WriteLine("{0}   has   entered   the   protected   area",    
                          Thread.CurrentThread.Name);  
   
                  //   Place   code   to   access   non-reentrant   resources   here.  
   
                  //   Simulate   some   work.  
                  Thread.Sleep(500);  
   
                  Console.WriteLine("{0}   is   leaving   the   protected   area\r\n",    
                          Thread.CurrentThread.Name);  
                     
                  //   Release   the   Mutex.  
                  mut.ReleaseMutex();  
          }  
  }  
  Top

相关问题

  • 请教Mutex的用法(100分)
  • &*用法~~
  • 请教多线程同步中的互斥对像Mutex的用法??(高手入)
  • setfocus的用法
  • icmp.dll的用法
  • Recordset的用法。
  • DataGrid的用法?
  • CDONTS.dll的用法???
  • EnumChildWindows的用法?
  • ListView的用法?

关键词

  • runtime
  • hmutex
  • interopservices
  • intptr
  • dllimport
  • kernel32
  • static extern
  • private

得分解答快速导航

  • 帖主:happyie
  • jinjazz
  • pupo

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo