有没有什么办法设定应用程序在指定的时间自动重新启动呀??? ( 积分: 100 )

J

jsw0525

Unregistered / Unconfirmed
GUEST, unregistred user!
有没有什么办法设定应用程序在指定的时间自动重新启动呀???
 
有没有什么办法设定应用程序在指定的时间自动重新启动呀???
 
再写一个程序在后台运行,等时间到就启动应用程序
 
给你个思路:<br>时间控制可以用Timer控件,我给你一个程序关闭,然后又自己打开的一段代码,主要是以Bat的方式来执行:<br>var<br> &nbsp;F: TextFile;<br> &nbsp;sPath, sBat: string;<br>begin<br> &nbsp;close;//关闭程序<br> &nbsp;sPath := Trim(ExtractFilePath(Application.ExeName));<br> &nbsp;sBat := sPath + '~ExecSelf.bat';<br> &nbsp;AssignFile(F, sBat);//创建bat文件<br> &nbsp;Rewrite(F);<br> &nbsp;Writeln(F, sFile); //把程序路径写入bat文件<br> &nbsp;Writeln(F, 'del %0');//bat文件执行完自动删除本身<br> &nbsp;CloseFile(F);<br> &nbsp;winexec(Pchar(sBat), SW_HIDE);//执行bat文件<br>end;
 
如果你的程序已启动,则可在程序里实现(最简单的是用Timer);还可用Windows的“任务计划”嘛。
 
首先,非常感谢刚才发言的各位大虾!<br>可能我刚才还没有说清楚我的意思,我的意思是:<br>我有一个程序是一天24小时都要运行的,不能停止。但由于常长时间运行,程序运行的效率就降低了,所以想通过重新启动该应用程序来提高其运行速度。我的想法是让该程序每一个月的月底的那一天的凌晨就让系统自动先自动关掉这个程序,然后再自动重新启动这个程序。不要我们自己去手动了,这样就达到了目的。不知道我是否说清楚了,谢谢各位!
 
注意:<br>我的程序原来已经启动了的。现在要首先关掉它,再重新启动它。谢谢<br>dreamisx,你能具体给我说说吗?最好给出具体的例子,谢谢
 
在你的这个程序里面在关闭自己的之前先启动另一程序,<br>另一程序启动以后立即执行你的主程序,然后把自己关闭。
 
运行程序的语句 <br>winexec(pchar(ExtractFilePath(Application.ExeName)+'ProcessWatch.exe'),sw_hide);
 
TO:wanghaiou<br>在你的这个程序里面在关闭自己的之前先启动另一程序,<br>另一程序启动以后立即执行你的主程序,然后把自己关闭。 &nbsp;<br><br><br>这样怎么控制哦,我用的是sockets通讯程序,如果原来的还没有完全关闭掉,又启动一个新的一样的程序,通讯会出错的。必须要把原来的程序彻底关闭掉,再重新启动才可以 。<br>你能否给点具体的代码给我?真的感谢
 
或者编写代码,首先检测这个程序是否在运行,如果在运行,则首先关闭。然后再启动。<br>我就再将这个程序放到“计划任务”里面去就可以了<br>(注意:关键是告诉我如何写代码检测某个程序正在运行,如运行就将这个正在运行的程序关闭,最后再重新启动运行)
 
參照這個吧.:)<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=3099103
 
在你的主程序的主窗体的Destroy事件里写上<br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br> &nbsp; winexec('c:/setup.exe',sw_hide);<br>end;<br><br>只有这个程序已经完全关闭以后才会执行'c:/setup.exe'<br>同样你在 从程序主窗体的Destroy事件里也写上<br><br>winexec(主程序,sw_hide);<br><br>然后在从程序的主窗体上放个时钟控件<br>写上<br> procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br> &nbsp; close;<br>end;<br>即可
 
我说了呀<br>关键要告诉我:如何写代码检测某个程序正在运行,如运行就将这个正在运行的程序关闭(要系统自己关闭自己),最后再重新启动运行
 
强调的都是自动的,是无人操作的。不要人管的。<br>到一定的时候它自己就会自动关闭,然后再马上自动重新启动。不知道我是否说清楚了。<br>谢谢各位
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, Buttons,TlHelp32;<br><br>type<br> ///////////////////////////////<br> &nbsp;TProcessInfo=Record<br> &nbsp; &nbsp; &nbsp; ExeFile:string;<br> &nbsp; &nbsp; &nbsp; ProcessID:Dword;<br> &nbsp; end;<br> &nbsp;ProcessInfo=^TProcessInfo;//////////<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;BitBtn1: TBitBtn;<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>//////////////////////////////////////////////////////////////////////////////<br>//11.取得当前进程列表<br>//////////////////////////////////////////////////////////////////////////////<br>procedure ProcessList(Var pList:TList);<br>var p:processInfo;<br> &nbsp; &nbsp;ok:Bool;<br> &nbsp; &nbsp;ProcessListHandle:THandle;<br> &nbsp; &nbsp;ProcessStruct:TProcessEntry32;<br>begin<br> &nbsp;pList:=TList.Create;<br> &nbsp;pList.Clear;<br> &nbsp;ProcessListHandle:=CreateToolHelp32SnapShot(TH32cs_SnapProcess,0);<br> &nbsp;ProcessStruct.dwSize:=SizeOf(ProcessStruct);<br> &nbsp;ok:=Process32First(ProcessListHandle,ProcessStruct);<br> &nbsp;while integer(ok)&lt;&gt;0 do<br> &nbsp;begin<br> &nbsp; &nbsp;New(p);<br> &nbsp; &nbsp;p.ExeFile:=ProcessStruct.szExeFile;<br> &nbsp; &nbsp;p.ProcessID:=ProcessStruct.th32ProcessID;<br> &nbsp; &nbsp;pList.Add(p);<br> &nbsp; &nbsp;ok:=Process32Next(ProcessListHandle,ProcessStruct);<br> &nbsp;end;<br> // dispose(p); &nbsp;//释放内存<br>end;<br>//////////////////////////////////////////////////////////////////////////////<br>//12.终止某一正在运行的进程<br>//////////////////////////////////////////////////////////////////////////////<br>procedure HideProcess(ProcessName:string);<br>var h:Thandle;<br> &nbsp; &nbsp;a:Dword;<br> &nbsp; &nbsp;p:processInfo;<br> &nbsp; &nbsp;i:integer;<br> &nbsp; &nbsp;Current:TList;<br>begin<br> &nbsp;ProcessList(current);<br> &nbsp;for i:=0 to current.Count-1 do<br> &nbsp;begin<br> &nbsp; &nbsp;p:=Current.Items;<br> &nbsp; &nbsp;if ansiuppercase(p.ExeFile)=ansiuppercase(ProcessName) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;h:=OpenProcess(Process_All_Access,true,p.ProcessID);<br> &nbsp; &nbsp; &nbsp;GetExitCodeProcess(h,a);<br> &nbsp; &nbsp; &nbsp;TerminateProcess(h,a);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br>////////////////////////////////////////////////////////////////////////////////<br>//13.查找进程<br>////////////////////////////////////////////////////////////////////////////////<br>function FindProcess(ProcessName:string):Bool;<br>var<br> &nbsp;p:processInfo;<br> &nbsp;i:integer;<br> &nbsp;Current:TList;<br>begin<br> &nbsp;result:=false;<br> &nbsp;ProcessList(current);<br> &nbsp;for i:=0 to current.Count-1 do<br> &nbsp;begin<br> &nbsp; &nbsp;p:=Current.Items;<br> &nbsp; &nbsp;if ansiuppercase(p.ExeFile)=ansiuppercase(ProcessName) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;result:=true;<br> &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;Current.Free;<br> &nbsp;dispose(p); &nbsp;//释放内存<br>end;<br>end.
 
恩,谢谢你们。我先静下心来看看。可以了马上给分。<br>你们可以给简单一点不,看上去怎么这么复杂呀<br>谢谢<br>等分就是了
 
流程是<br>1.在主程序的主窗体上放一个时钟控件<br> 在时钟空间里面写代码,随时判断当前日期和时间是否慢足重起条件,如果满足重起条件就执行 close;<br>2 在你的主程序的主窗体的Destroy事件里写上<br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br> &nbsp; winexec('从程序',sw_hide);<br>end;<br>这样 在主程序关闭以后就会运行你的重程序<br>3.作一个从程序<br>在从程序的主窗体的Destroy事件里写上<br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br> &nbsp; winexec('主程序',sw_hide);<br>end;<br>4.在从程序的主窗体上放一个时钟控件<br> 在时钟空间里面写代码close;<br><br>这个步骤一定可行的
 
to:wanghaiou, <br>真的好感谢你!!!谢谢<br>开始有带内细节我没有给你说清楚,就是:<br>是这样的,那个服务程序是VC写的。<br>现在我想用delphi来实现刚才说的这功能,写点程序让VC这服务器程序在每个月的30号的凌晨2点钟(这时没有人用这程序)自动关闭这服务器程序,再马上自动重新启动起来运行。
 
我开始有点不明白你的意思了
 
顶部