博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
防止程序重复执行 Controling the number of application instances
阅读量:6457 次
发布时间:2019-06-23

本文共 3477 字,大约阅读时间需要 11 分钟。

{
Article: Controling the number of application instances http://delphi.about.com/library/weekly/aa100703a.htm In this article you'll learn how to "run-once enable" a Delphi application that can check for its previous (running) instance. Along the process, several techniques of implementing such a check will be discussed; as well as how to bring your already running application to the foreground, if a user tries to run it "one more time". By the end of the article you'll have a copy-to-go code to control the behavior of your application's multiple instances: with the option to limit the number of running instances.}unit uCheckPrevious;interfaceuses Windows, SysUtils;function RestoreIfRunning( const AppHandle : THandle; MaxInstances : integer = 1 ) : boolean;implementationtype PInstanceInfo = ^TInstanceInfo; TInstanceInfo = packed record PreviousHandle : THandle; RunCounter : integer; end;var MappingHandle : THandle; InstanceInfo : PInstanceInfo; MappingName : string; RemoveMe : boolean = True;function RestoreIfRunning( const AppHandle : THandle; MaxInstances : integer = 1 ) : boolean;begin Result := True; MappingName := StringReplace( ParamStr( 0 ), '\', '', [ rfReplaceAll, rfIgnoreCase ] ); MappingHandle := CreateFileMapping( $FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf( TInstanceInfo ), PChar( MappingName ) ); if MappingHandle = 0 then RaiseLastOSError else begin if GetLastError <> ERROR_ALREADY_EXISTS then begin InstanceInfo := MapViewOfFile( MappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf( TInstanceInfo ) ); InstanceInfo^.PreviousHandle := AppHandle; InstanceInfo^.RunCounter := 1; Result := False; end else // already runing begin MappingHandle := OpenFileMapping( FILE_MAP_ALL_ACCESS, False, PChar( MappingName ) ); if MappingHandle <> 0 then begin InstanceInfo := MapViewOfFile( MappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf( TInstanceInfo ) ); if InstanceInfo^.RunCounter >= MaxInstances then begin RemoveMe := False; if IsIconic( InstanceInfo^.PreviousHandle ) then ShowWindow( InstanceInfo^.PreviousHandle, SW_RESTORE ); SetForegroundWindow( InstanceInfo^.PreviousHandle ); end else begin InstanceInfo^.PreviousHandle := AppHandle; InstanceInfo^.RunCounter := 1 + InstanceInfo^.RunCounter; Result := False; end end; end; end;end; (* RestoreIfRunning *)initialization// nothing special here// we need this section because we have the// finalization sectionfinalization// remove this instanceif RemoveMe thenbegin MappingHandle := OpenFileMapping( FILE_MAP_ALL_ACCESS, False, PChar( MappingName ) ); if MappingHandle <> 0 then begin InstanceInfo := MapViewOfFile( MappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf( TInstanceInfo ) ); InstanceInfo^.RunCounter := -1 + InstanceInfo^.RunCounter; end else RaiseLastOSError;end;if Assigned( InstanceInfo ) then UnmapViewOfFile( InstanceInfo );if MappingHandle <> 0 then CloseHandle( MappingHandle );end. (* unit uCheckPrevious *)
program XXX;uses  Forms,  ...  uCheckPrevious in 'uCheckPrevious.pas';{
$R *.RES}begin if not uCheckPrevious.RestoreIfRunning( Application.Handle ) then begin Application.Initialize; Application.Title := 'Hex Editor'; Application.CreateForm( TMain, Main ); Application.Run; end;end.

 

转载地址:http://cnnzo.baihongyu.com/

你可能感兴趣的文章
在排序数组中,找出给定数字的出现次数
查看>>
javascript object-oriented
查看>>
Android性能优化---布局优化
查看>>
了解PHP中$_SERVER变量对路径的解析
查看>>
第二个小应用——简易便签
查看>>
apache cxf之 一个简单的JAX-WS服务程序
查看>>
.NET:用T4消除代码重复,对了,也错了
查看>>
android编译make错误——"javalib.jar invalid header field”、"classes-full-debug.jar 错误 41 "...
查看>>
点击threadItem查看MessageList时传递数据
查看>>
创建第一个ArcGIS API for Silverlight应用
查看>>
android.telephony.SmsManager.sendMultipartTextMessage
查看>>
Wheel ProgressBar 实现之三——模拟进度过程
查看>>
MAC下安装MySQL
查看>>
crtmpserver 在VS2010下的build
查看>>
过压保护(2)
查看>>
Android UI基础教程 目录
查看>>
美食杂志排行榜_百度知道
查看>>
让自制脚本随系统开机运行
查看>>
linux修改yum本地源的方法
查看>>
黄聪:WordPress默认编辑器可视化切换不见了,非插件导致消失问题
查看>>