//开发中常用的只允许一个程序运行的办法 //程序以单例模式运行 常用办法,创建一个互斥量 //由于互斥量只允许一个进程或者线程占用 会创建失败,利用这个特性可以做到单例运行改程序 #include "stdafx.h" #include
#include
int _tmain(int argc, _TCHAR* argv[]) { HANDLE m_hMutex = CreateMutex(NULL,TRUE,L"{D5A500DF-E48C-4ba9-A91D-AA6DD4C309A3}"); DWORD dwRet = GetLastError(); if (m_hMutex) { if (ERROR_ALREADY_EXISTS == dwRet) { printf("程序已经在运行中了,程序退出!\n"); CloseHandle(m_hMutex); return 0; } } else { printf("创建互斥量错误,程序退出!\n"); CloseHandle(m_hMutex); return 0; } printf("RUNING....!\n"); system("pause"); CloseHandle(m_hMutex); return 0; }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/227907.html原文链接:https://javaforall.net
