多線程編程(11) – 多線程同步之 Mutex (互斥對象)[續]

Mutex 作為系統核心對象是可以跨進程的(臨界區就不行), 我們可以利用互斥對象禁止程式重複啟動.工作思路:先用 OpenMutex 嘗試開啟一個自訂名稱的 Mutex 對象, 如果開啟失敗說明之前沒有這個對象存在;如果之前沒有這個對象, 馬上用 CreateMutex 建立一個, 此時的程式應該是第一次啟動;再重複啟動時, 那個 OpenMutex 就有結果了, 然後強制退出.最後在程式結束時用 CloseHandle 釋放 Mutex 對象.function OpenMutex(

WinAPI: ExtractIcon – 擷取 EXE、DLL 或 ICO 檔案中的表徵圖

//聲明:ExtractIcon( hInst: HINST; {調用函數的程式執行個體} lpszExeFileName: PChar; {檔案路徑; 檔案可以是 *.exe、*.dll、*.ico} nIconIndex: UINT {表徵圖索引}): HICON; {返回表徵圖控制代碼; 索引為 0 時返回第一個表徵圖控制代碼; 索引為 #FFFFFFFF 時返回表徵圖總數}//舉例:unit Unit1;interfaceuses

多線程編程(12) – 多線程同步之 Semaphore (訊號對象)

之前已經有了兩種多線程的同步方法:CriticalSection(臨界區) 和 Mutex(互斥), 這兩種同步方法差不多, 只是範圍不同;CriticalSection(臨界區) 類似於只有一個蹲位的公用廁所, 只能一個個地進; Mutex(互斥) 對象類似於接力賽中的接力棒, 某一時刻只能一個人持有, 誰拿著誰跑.什麼是 Semaphore(訊號或叫訊號量)呢?譬如到銀行辦業務、或者到車站買票, 原來只有一個服務員, 不管有多少人排隊等候, 業務只能一個個地來.假如增加了業務視窗,

使用 System.Sin、System.Cos 函數畫圓 – 繪製五環表徵圖

本例: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:

多線程編程(13) – 多線程同步之 Event (事件對象)

據說 Event(事件對象) 是多線程最原始的同步手段, 我覺得它是最靈活的一個.Event 對象(的控制代碼表)中主要有兩個布爾變數, 從它的建立函數中可以看得清楚:function CreateEvent( lpEventAttributes: PSecurityAttributes; {安全設定} bManualReset: BOOL; {第一個布爾} bInitialState: BOOL;

WinAPI: LoadString – 從資源中載入字串

//聲明:LoadString( hInstance: HINST; {EXE 或 DLL 的控制代碼} uID: UINT; {資源 ID} lpBuffer: PChar; {緩衝區} nBufferMax: Integer {緩衝區大小}): Integer; {返回字串實際長度}//假如有這樣一個字串資源: StringTablebegin0 "String_One"1 "String_Two"end{上面給出了資源源檔案,

多線程編程(14) – 多線程同步之 WaitableTimer (等待定時器對象)

function CreateWaitableTimer( lpTimerAttributes: PSecurityAttributes; {安全} bManualReset: BOOL; {True: 可調度多個線程; False: 只調度一個線程} lpTimerName: PWideChar {名稱}): THandle; stdcall; {返回控制代碼}function SetWaitableTimer( hTimer: THandle;

學習 TTreeView [14] – StateIndex(狀態表徵圖)、OverlayIndex(疊加表徵圖)

先給 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 關鍵字的小練習

//例子中的三個變數讓 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,

多線程編程(10) – 多線程同步之 Mutex (互斥對象)

原理分析:互斥對象是系統核心對象, 各線程都可以擁有它, 誰擁有誰就能執行; 執行完畢, 用 ReleaseMutex 函數釋放擁有權, 以讓其他等待的線程使用.其他線程可用 WaitForSingleObject 函數排隊等候(等候也可以理解為排隊申請).使用過程:var hMutex: THandle; {應該先聲明一個全域的互斥控制代碼}CreateMutex {建立一個互斥對象}WaitForSingleObject {用等待函數排隊等候}ReleaseMutex

學習 TTreeView [12] – FullExpand、FullCollapse、GetNodeAt

本例: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

WinAPI: SetRect 及初始化矩形的幾種辦法

本例分別用五種辦法初始化了同樣的一個矩形, 運行: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:

多線程編程(16) – 多線程同步之 WaitableTimer (等待定時器對象)[續二]

喝酒醉了一天, 重裝系統一天, 兩天沒上部落格了; 繼續學習...想過沒有? WaitableTimer 是在 "定時等待", 前面例子中的 WaitForSingleObject 等待函數 "也在等待", 這就 "雙重等待" 了, 這不好, 太浪費資源.其實作為同步工具, 前面的幾種方法(事件、訊號、臨界區)基本夠用了; WaitableTimer 的作用並不是為了重複前面的功能, 它的主要功用類似 TTimer 類;

學習 TTreeView [15] – 串連資料庫 (作為給 “丁永其” 和 “DELPHI萬歲” 兩位朋友的回複)

本例: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:

多線程編程(17) – 多線程同步之 WaitableTimer (等待定時器對象)[續三]

根據 WaitableTimer 的主要功用, 現在再把它放在 "線程同步" 的話題中討論有點不合適了, 就要結束它.//重新看看那個 APC 回呼函數的格式:procedure TimerAPCProc( lpArgToCompletionRoutine: Pointer; dwTimerLowValue, dwTimerHighValue: DWORD); stdcall;TimerAPCProc 的後兩個參數其實是在傳遞一個值, 使用時要把它們合并為一個 TFileTime

非官方的 TWaitableTimer 類

unit WTimer;interfaceuses Windows, SysUtils, SyncObjs;type TWaitableTimer = class(TSynchroObject) protected FHandle: THandle; FPeriod: LongInt; FDueTime: TDateTime; FLastError: Integer; FLongTime: Int64; public constructor

學習 TTreeView [16] – 給 TTreeView 添加複選框 (回複 “丁永其” 的問題)

問題來源: 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:

總頁數: 61357 1 .... 4762 4763 4764 4765 4766 .... 61357 Go to: 前往

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.