我也很无语,也不想这个么干,只是.NET CF v2是这也不支持,那也不支持.

前段时间要做一个只让程序启动一次的东西,进入我大脑的第一个词汇就是互斥量(信号量的特殊化). 网上搜了一下确实也有人用互斥量实现我的那个需求.

动手做起来才发现不太对劲,.NET CF v2 的互斥量是不支持命名的,没名字就不能用于IPC(进程间通讯),那还有毛用......

翻了翻MSDN,写了一个Mutex,还挺好用,其实就那么几个API :

        sealed class NativeMethod
        {
            
public const UInt32 WAIT_OBJECT_0 = 0x00000000;
            
public const UInt32 WAIT_TIMEOUT = 258;
            
public const UInt32 WAIT_FAILED = 0xffffffff;

            [DllImport(
"coredll.Dll", SetLastError = true)]
            
public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, int bInitialOwner, string lpName);

            [DllImport(
"coredll.Dll", SetLastError = true)]
            
public static extern UInt32 ReleaseMutex(IntPtr hMutex);

            
//WAIT_OBJECT_0 The state of the specified object is signaled. 
            
//WAIT_TIMEOUT The time-out interval elapsed, and the object's state is nonsignaled. 
            [DllImport("coredll.Dll", SetLastError = true)]
            
public static extern UInt32 WaitForSingleObject(IntPtr hHandle, int dwMilliseconds);

            [DllImport(
"coredll.Dll", SetLastError = true)]
            
public static extern UInt32 CloseHandle(IntPtr hObject); 
        }

相关文章:

  • 2022-12-23
  • 2021-12-02
  • 2021-05-17
  • 2021-12-01
  • 2022-12-23
  • 2022-01-09
  • 2021-09-20
猜你喜欢
  • 2021-05-28
  • 2021-05-29
  • 2021-05-01
  • 2021-12-12
  • 2022-12-23
相关资源
相似解决方案