如何避免一个程序的多个例程同时运行?(100分)

  • 主题发起人 主题发起人 FreeFish
  • 开始时间 开始时间
F

FreeFish

Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序里想避免同时运行该程序的多个例程(因为该程序含有一个闪<br>现窗口,我将防止运行多个例程的代码写在闪现窗口的OnCreate事件里)。<br>虽然这样也能够避免了多个例程同时运行,但每运行一次此程序,闪现<br>窗口也跟着出现。请问有什么好的办法可以避免这种现象的发生,即在<br>内存中没有运行此程序的情况下,闪现窗口正常出现;而在内存中已经<br>运行了该程序的情况下,闪现窗口不会出现。而无论在那种情况下,都<br>要保证整个系统只能有一个例程在运行。以上情况与"金山词霸2001"启<br>动时情况相似。请问各位高手能否有其它更好的办法实现?(附程序代码)<br><br>//以下为项目文件Project1<br><br>Uses<br>&nbsp; &nbsp;Forms,<br>&nbsp; &nbsp;Unit1 in 'Unit1.pas' {MainForm},<br>&nbsp; &nbsp;Unit2 in 'Unit2.pas' {SplashForm};<br><br>{$R*.RES}<br><br>begin<br><br>&nbsp; &nbsp;Application.Initialize;<br>&nbsp; &nbsp;SplashForm:=TSplashForm.Create(Application);<br>&nbsp; &nbsp;SplashForm.Show;<br>&nbsp; &nbsp;SplashForm.Update;<br>&nbsp; &nbsp;//SplashForm中放置了TTimer组件,其Enabled属性为True,<br>&nbsp; &nbsp;//其Interval属性为2500,其OnTimer事件代码<br>&nbsp; &nbsp;// 为:DelayTimer.Enabled:=False<br>&nbsp; &nbsp;While (SplashForm.DelayTimer.Enabled) do<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessage;<br>&nbsp; &nbsp;Application.CreateForm(TMainForm,MainForm);<br>&nbsp; &nbsp;SplashForm.Hide;<br>&nbsp; &nbsp;SplashForm.Free;<br>&nbsp; &nbsp;Application.Run;<br><br>end.<br><br>//以下为单元文件Unit2<br><br>procedure TSplashForm.FormCreate(Sender:TObject);<br>var<br>&nbsp; &nbsp;S:string;<br>&nbsp; &nbsp;HOldWindow:HWND;<br>&nbsp; &nbsp;AppName:array[0..255] of char;<br>begin<br>&nbsp; &nbsp;S:=Application.Title;<br>&nbsp; &nbsp;StrpCopy(AppName,S);<br>&nbsp; &nbsp;Application.Title:='wjy'+inttostr(HInstance);<br>&nbsp; &nbsp;HOldWindow:=FindWindow(nil,AppName);<br>&nbsp; &nbsp;Application.Title:=S;<br>&nbsp; &nbsp;if HOldWindow&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowWindow(HOldWindow,SW_Restore);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.Terminate; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; end;<br>end;
 
不要放在Splash窗口的create事件中,直接放在TApplication的启动代码中,<br><br>//以下为项目文件Project1<br><br>Uses<br>&nbsp; &nbsp;Forms,<br>&nbsp; &nbsp;Unit1 in 'Unit1.pas' {MainForm},<br>&nbsp; &nbsp;Unit2 in 'Unit2.pas' {SplashForm};<br><br>{$R*.RES}<br><br>begin<br><br>&nbsp; &nbsp;Application.Initialize;<br><br>! &nbsp; 放在这里<br>!<br><br><br>&nbsp; &nbsp;SplashForm:=TSplashForm.Create(Application);<br>&nbsp; &nbsp;SplashForm.Show;<br>&nbsp; &nbsp;SplashForm.Update;<br>&nbsp; &nbsp;//SplashForm中放置了TTimer组件,其Enabled属性为True,<br>&nbsp; &nbsp;//其Interval属性为2500,其OnTimer事件代码<br>&nbsp; &nbsp;// 为:DelayTimer.Enabled:=False<br>&nbsp; &nbsp;While (SplashForm.DelayTimer.Enabled) do<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessage;<br>&nbsp; &nbsp;Application.CreateForm(TMainForm,MainForm);<br>&nbsp; &nbsp;SplashForm.Hide;<br>&nbsp; &nbsp;SplashForm.Free;<br>&nbsp; &nbsp;Application.Run;<br><br>end.<br>
 
不要那么麻烦,在.dpr文件中就能实现<br>var<br>&nbsp; hmutex:hwnd;<br>&nbsp; ret:integer;<br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; hmutex:=createmutex(nil,false,'project1');<br>&nbsp; ret:=getlasterror;<br>&nbsp; if ret&lt;&gt;error_already_exists then<br>&nbsp; begin<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; releasemutex(hmutex);
 
我的例子,将判断写在项目文件里不就可以了吗?<br>如何让程式只执行一份?<br>&nbsp;program DEL3test;<br>&nbsp;uses<br>&nbsp;Forms,Windows,SysUtils,<br>&nbsp;DEL3unit in 'DEL3unit.pas' {Form1};<br>&nbsp;{$R *.RES}<br>&nbsp;Var<br>&nbsp; &nbsp; hMutex:HWND;<br>&nbsp; &nbsp; Ret:Integer;<br>&nbsp;begin<br>&nbsp; Application.Initialize;<br>&nbsp; Application.Title := 'aaaaaa';<br>&nbsp; hMutex:=CreateMutex(nil,False,'aaaaaa');<br>&nbsp; Ret:=GetLastError;<br>&nbsp; If Ret&lt;&gt;ERROR_ALREADY_EXISTS Then<br>&nbsp; &nbsp;Begin<br>&nbsp; &nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; &nbsp; Application.Run;<br>&nbsp; &nbsp;End<br>&nbsp; Else<br>&nbsp; &nbsp;Application.MessageBox('Run Twice!','Notes!',MB_OK);<br>&nbsp; ReleaseMutex(hMutex);<br>&nbsp;end.<br>
 
多人接受答案了。
 
Terry_lzs和Li2两位大侠,我用了上述的方法,的确是可以防止多个例程同时<br>运行,但后来又遇上了一个小小的问题:即当第一个例程处于最小化的时候,<br>再次运行该程序并不能够激活第一个例程并使之恢复到原始窗口大小?
 
这个也没有什么问题,不过你要自己写代码,没有现成的函数或者是过程。自己定义消息传<br>送给第一个例程,把代码添加在releasemutex(hmutex)前面就行。<br>
 
后退
顶部