Mutex 作為系統核心對象是可以跨進程的(臨界區就不行), 我們可以利用互斥對象禁止程式重複啟動.工作思路:先用 OpenMutex 嘗試開啟一個自訂名稱的 Mutex 對象, 如果開啟失敗說明之前沒有這個對象存在;如果之前沒有這個對象, 馬上用 CreateMutex 建立一個, 此時的程式應該是第一次啟動;再重複啟動時, 那個 OpenMutex 就有結果了, 然後強制退出.最後在程式結束時用 CloseHandle 釋放 Mutex 對象.function OpenMutex(
//聲明:ExtractIcon( hInst: HINST; {調用函數的程式執行個體} lpszExeFileName: PChar; {檔案路徑; 檔案可以是 *.exe、*.dll、*.ico} nIconIndex: UINT {表徵圖索引}): HICON; {返回表徵圖控制代碼; 索引為 0 時返回第一個表徵圖控制代碼; 索引為 #FFFFFFFF 時返回表徵圖總數}//舉例:unit Unit1;interfaceuses
之前已經有了兩種多線程的同步方法:CriticalSection(臨界區) 和 Mutex(互斥), 這兩種同步方法差不多, 只是範圍不同;CriticalSection(臨界區) 類似於只有一個蹲位的公用廁所, 只能一個個地進; Mutex(互斥) 對象類似於接力賽中的接力棒, 某一時刻只能一個人持有, 誰拿著誰跑.什麼是 Semaphore(訊號或叫訊號量)呢?譬如到銀行辦業務、或者到車站買票, 原來只有一個服務員, 不管有多少人排隊等候, 業務只能一個個地來.假如增加了業務視窗,
本例:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure FormCreate(Sender: TObject); procedure FormMouseUp(Sender:
據說 Event(事件對象) 是多線程最原始的同步手段, 我覺得它是最靈活的一個.Event 對象(的控制代碼表)中主要有兩個布爾變數, 從它的建立函數中可以看得清楚:function CreateEvent( lpEventAttributes: PSecurityAttributes; {安全設定} bManualReset: BOOL; {第一個布爾} bInitialState: BOOL;
//聲明:LoadString( hInstance: HINST; {EXE 或 DLL 的控制代碼} uID: UINT; {資源 ID} lpBuffer: PChar; {緩衝區} nBufferMax: Integer {緩衝區大小}): Integer; {返回字串實際長度}//假如有這樣一個字串資源: StringTablebegin0 "String_One"1 "String_Two"end{上面給出了資源源檔案,
function CreateWaitableTimer( lpTimerAttributes: PSecurityAttributes; {安全} bManualReset: BOOL; {True: 可調度多個線程; False: 只調度一個線程} lpTimerName: PWideChar {名稱}): THandle; stdcall; {返回控制代碼}function SetWaitableTimer( hTimer: THandle;
先給 ImageList1 添加表徵圖如下:關於 OverlayIndex 的提示:在給一個節點指定 OverlayIndex 以前, 需要先用 ImageList1.Overlay 指定可用的 OverlayIndex 號.測試:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, ImgList,
問題來源: http://www.cnblogs.com/del/archive/2008/06/27/1197961.html#1237665unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton;
//例子中的三個變數讓 absolute 都給弄到一塊了, 這有時會很方便; 但我總也想不到用, 還是不熟.procedure TForm1.FormCreate(Sender: TObject);var num: Int64; pt: TPoint absolute num; arr: array[0..1] of Integer absolute pt;begin pt.X := 111; pt.Y := 222; ShowMessageFmt('%d, %d', [arr[0]
首先建議初學者不要在這些小的技巧上下太多功夫, 學好基礎才是根本;我的部落格上這種東西不多, 這是大家討論時, 話趕話趕出來的, 知道有這種可行性即可, 沒有多少實用價值.本例:此例回複來賓在http://www.cnblogs.com/del/archive/2008/02/26/1081644.html 碰到的問題.全部代碼如下(測試時注意給主表單焦點):unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants,
原理分析:互斥對象是系統核心對象, 各線程都可以擁有它, 誰擁有誰就能執行; 執行完畢, 用 ReleaseMutex 函數釋放擁有權, 以讓其他等待的線程使用.其他線程可用 WaitForSingleObject 函數排隊等候(等候也可以理解為排隊申請).使用過程:var hMutex: THandle; {應該先聲明一個全域的互斥控制代碼}CreateMutex {建立一個互斥對象}WaitForSingleObject {用等待函數排隊等候}ReleaseMutex
本例:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls;type TForm1 = class(TForm) TreeView1: TTreeView; Button1: TButton; Button2: TButton; procedure
本例分別用五種辦法初始化了同樣的一個矩形, 運行:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4:
本例:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;type TForm1 = class(TForm) procedure FormPaint(Sender: TObject); procedure FormClick(Sender: TObject); end;var Form1:
喝酒醉了一天, 重裝系統一天, 兩天沒上部落格了; 繼續學習...想過沒有? WaitableTimer 是在 "定時等待", 前面例子中的 WaitForSingleObject 等待函數 "也在等待", 這就 "雙重等待" 了, 這不好, 太浪費資源.其實作為同步工具, 前面的幾種方法(事件、訊號、臨界區)基本夠用了; WaitableTimer 的作用並不是為了重複前面的功能, 它的主要功用類似 TTimer 類;
本例:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, DBGrids, DB, DBClient, StdCtrls, ComCtrls, ExtCtrls;type TForm1 = class(TForm) DataSource1: TDataSource; ClientDataSet1:
根據 WaitableTimer 的主要功用, 現在再把它放在 "線程同步" 的話題中討論有點不合適了, 就要結束它.//重新看看那個 APC 回呼函數的格式:procedure TimerAPCProc( lpArgToCompletionRoutine: Pointer; dwTimerLowValue, dwTimerHighValue: DWORD); stdcall;TimerAPCProc 的後兩個參數其實是在傳遞一個值, 使用時要把它們合并為一個 TFileTime
unit WTimer;interfaceuses Windows, SysUtils, SyncObjs;type TWaitableTimer = class(TSynchroObject) protected FHandle: THandle; FPeriod: LongInt; FDueTime: TDateTime; FLastError: Integer; FLongTime: Int64; public constructor
問題來源: http://www.cnblogs.com/del/archive/2008/05/15/1114450.html#1199402本例:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls;type TForm1 = class(TForm) TreeView1: