其實跨行程的溝通也不必那麼的麻煩, 最簡單的方式是在特定的Registry中寫入資料, 主<br>進程中不斷的判斷有沒有寫入該特定的值就可以了, 例如你可以將主程式的Application <br>Handle寫到Registry中!<br>如果要Event的用法, 以下有範例<br><br>//建立Event, 並等待Event被設定<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br> TForm1 = class(TForm)<br> procedure FormActivate(Sender: TObject);<br> private<br> { Private declarations }<br> FEventHandle: THandle;<br> FLoaded: boolean;<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.FormActivate(Sender: TObject);<br>begin<br> if not FLoaded then<br> begin<br> FLoaded := True;<br> FEventHandle := CreateEvent(nil, False, False, 'XXXEvent');<br> while not Application.Terminated do<br> begin<br> Application.ProcessMessages;<br> if WaitForSingleObject(FEventHandle, 10) = WAIT_OBJECT_0 then<br> begin<br> ShowMessage('Wait for');<br> break;<br> end;<br> end;<br> CloseHandle(FEventHandle);<br> if not Application.Terminated then Close;<br> end;<br>end;<br><br>end.<br><br>//另一個程式, 以OpenEvent打開Event, 並設定它, 注意, 名稱要與上一程式<br>//CreateEvent的名稱一樣<br>unit Unit2;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm2 = class(TForm)<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form2: TForm2;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm2.Button1Click(Sender: TObject);<br> var EventHandle: THandle;<br>begin<br> EventHandle := OpenEvent(EVENT_MODIFY_STATE, False, 'XXXEvent');<br> SetEvent(EventHandle);<br> CloseHandle(EventHandle);<br>end;<br><br>end.<br>