讓程式不能被多次執行

2021-07-23 04:15:46 字數 533 閱讀 2229

《朱老師物聯網大講堂》學習筆記       

讓程式不能被多次執行的技巧,

因為守護程序是長時間執行而不退出,所以多次執行就不太好了,甚至會帶來錯誤,

實現方法,

程式執行之初判定乙個特定檔案存在與否,若存在則程序已經在執行,若不存在則程序沒有執行,並建立這個檔案,

這個檔名,要特殊古怪點,

如下是朱老師上課示例**,

#include #include #include #include #include #include #define file	"/var/aston_test_single"

void delete_file(void);

int main(void) }

atexit(delete_file); // 註冊程序清理函式

int i = 0;

for (i=0; i<10; i++)

return 0;

}void delete_file(void)

防止程式被多次執行的API函式

下面這個例子找出程式是否在執行,你可作乙個處理,如果在執行就退出.declare function findwindow lib user32 alias findwindowa byval lpclassname as string,byval lpwindowname as string as ...

Delphi 防止程式多次執行

program project1 uses forms,windows,unit1 in unit1.pas var hmutex hwnd ret integer begin hmutex createmutex nil,false,test ret getlasterror if ret err...

C 防止程式多次執行

經過我的測試,還比較好用,但是有個問題,如果不登出,用另乙個使用者進入,則程式不能判斷出已執行。所以只限於用在單使用者環境,還是不太完美。class program console.writeline 正在執行中 console.readline public static class oneins...