防止程序运行多个例程? <br>More than one instance of program?<br>回答<br> This is copied direct from my *.dpr file. You can work it for your own<br>use.<br>var<br> hMutex : Thandle;<br> WaitResult : word;<br> BroadcastList : DWORD;<br>begin<br> MessageID := RegisterWindowMessage('Check For Choice Previous Inst');<br>// register a message to use later on<br> hMutex := createMutex(nil,false,pchar('App_Choice')); // grab a mutex<br>handle<br> WaitResult := WaitForSingleObject(hMutex,10); // wait to see<br>if we can have exclusive use of the mutex<br> if ( waitResult = WAIT_TIMEOUT ) then // if we can't then broadcast<br>the message to make the owner of the mutex respond<br> { request that the running application takes focus }<br> begin<br> BroadcastList := BSM_APPLICATIONS;<br> BroadcastSystemMessage(<br>BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0); //32 bit - broadcast the<br>message to all apps - only a prev inst will hear it.<br> end<br> else<br> begin<br> { do the normal stuff}<br> Application.Title := 'Choice Organics Purchase & Sales System';<br> Application.CreateForm(TMainForm, MainForm);<br> Application.Run;<br> ReleaseMutex(hMutex); // release the mutex as a politeness<br> end;<br> CloseHandle(hMutex); // close the mutex handle<br>end.<br>This goes in the MainForm<br>procedure Tmainform.OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);<br>begin<br>{ If it's the special message then focus on this window}<br>if Msg.Message = MessageID then // if we get the broadcast message from an<br>another instance of this app that is trying to start up<br> begin<br> show;<br> WindowState := wsMaximized;<br> BringToFront;<br> SetFocus;<br> Handled := true;<br> end;<br>end;<br>//And this goes in the TMainForm.FormCreate ;-<br>Application.OnMessage:= OnAppMessage;<br>