保證應用程式只有乙個例項在執行

2021-05-28 05:18:49 字數 899 閱讀 3634

要實現這樣的功能,方法灰常多,利用命名的事件物件,命名的互斥物件都可以實現,下面寫出這兩種方法的實現:

1.命名的互斥物件:

void main()

handle hthread1 ;

handle hthread2 ;

// 建立互斥物件

hmutex = createmutex(null, true, "tickets") ;

if(hmutex)

if(error_already_exists == getlasterror())

cout << "only one instance can run!" << endl ;

return ;

hthread1 = createthread(null, 0, fun1proc, null, 0, null) ;

hthread2 = createthread(null, 0, fun2proc, null, 0, null) ;

closehandle(hthread1) ;

closehandle(hthread2) ;

waitforsingleobject(hmutex, infinite) ;

releasemutex(hmutex) ;

releasemutex(hmutex) ;

sleep(4000) ;

2.命名的事件物件

g_hevent = createevent(null, false, false, "tickets") ;

if (g_hevent)

if (error_already_exists == getlasterror())

cout << "only ont instance can run!" << endl ;

return ;

用C 讓應用程式只有乙個例項在執行

原 很亂,整理了一下 using system using system.runtime.interopservices using system.windows.forms using system.diagnostics using system.reflection public class ...

保證只有乙個當前程式在執行

setlasterror 0 handle hmutex createmutex null,false,lpcstr l 5d0e714f 8c2e 94d3 e819 92c54fdd2edc if getlasterror error already exists setlasterror 0 ...

如何讓Qt應用程式只有乙個例項

在實際應用中,我們經常需要讓應用程式只有乙個例項,再開啟新的文件或者頁面時,只是替換現在的視窗或者新開啟乙個標籤,而不是重新啟動一次應用程式。qt中是否可以做到這樣呢,答案是肯定的,因為 qt本身可以直接呼叫系統 api,肯定可以做到,但是我們希望找到乙個跨平台的通用的解決方案。這就要用到qt的 q...