- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一天,我有了一个疯狂的想法,要制作一个全新的 TApplication
替代品来进行实验。我已经编译和运行了所有内容,它确实正确显示了主窗体,一切都响应良好,但是关闭窗体后,应用程序不会停止。我确定我从原始 Forms.pas TApplication (注册关闭事件)中复制了所有必要的内容,但我没有看到它工作。我必须以令人讨厌的方式终止调试 session 。
我在这个小实验中的目标是为非常简单的事情构建一个轻量级应用程序,而不是 TApplication 可以处理的所有可能的事情,而且主要是因为我在这个领域有一些很好的经验。
这是我现在拥有的单元,下面是它的实现。
unit JDForms;
interface
uses
Forms, Classes, SysUtils, StrUtils, Windows, Win7, XPMan, Variants,
Messages, Dialogs;
type
TJDForm = class;
TJDApplication = class;
TJDApplicationThread = class;
TJDForm = class(TCustomForm)
private
public
published
end;
TJDApplication = class(TComponent)
private
fRunning: Bool;
fTerminated: Bool;
fThread: TJDApplicationThread;
fMainForm: TJDForm;
fOnMessage: TMessageEvent;
fShowMainForm: Bool;
fHandle: HWND;
procedure ThreadTerminated(Sender: TObject);
procedure HandleMessage;
procedure ProcessMessages;
function ProcessMessage(var Msg: TMsg): Boolean;
procedure ThreadSync(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
property Thread: TJDApplicationThread read fThread;
procedure Initialize;
procedure Run;
procedure CreateForm(InstanceClass: TComponentClass; var Reference);
procedure Terminate;
property Terminated: Bool read fTerminated;
procedure HandleException(Sender: TObject);
property Handle: HWND read fHandle;
published
property ShowMainForm: Bool read fShowMainForm write fShowMainForm;
property OnMessage: TMessageEvent read fOnMessage write fOnMessage;
end;
TJDApplicationThread = class(TThread)
private
fOwner: TJDApplication;
fStop: Bool;
fOnSync: TNotifyEvent;
procedure DoSync;
protected
procedure Execute; override;
public
constructor Create(AOwner: TJDApplication);
destructor Destroy; override;
procedure Start;
procedure Stop;
published
property OnSync: TNotifyEvent read fOnSync write fOnSync;
end;
var
JDApplication: TJDApplication;
implementation
procedure DoneApplication;
begin
with JDApplication do begin
if Handle <> 0 then ShowOwnedPopups(Handle, False);
//ShowHint := False;
Destroying;
DestroyComponents;
end;
end;
{ TJDApplication }
constructor TJDApplication.Create(AOwner: TComponent);
begin
fRunning:= False;
fTerminated:= False;
fMainForm:= nil;
fThread:= TJDApplicationThread.Create(Self);
fThread.FreeOnTerminate:= True;
fThread.OnTerminate:= ThreadTerminated;
fShowMainForm:= True;
end;
procedure TJDApplication.CreateForm(InstanceClass: TComponentClass; var Reference);
var
Instance: TComponent;
begin
Instance:= TComponent(InstanceClass.NewInstance);
TComponent(Reference) := Instance;
try
Instance.Create(Self);
except
TComponent(Reference):= nil;
raise;
end;
if (fMainForm = nil) and (Instance is TForm) then begin
TForm(Instance).HandleNeeded;
fMainForm:= TJDForm(Instance);
end;
end;
procedure TJDApplication.HandleException(Sender: TObject);
begin
{
if GetCapture <> 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0);
if ExceptObject is Exception then
begin
if not (ExceptObject is EAbort) then
if Assigned(FOnException) then
FOnException(Sender, Exception(ExceptObject))
else
ShowException(Exception(ExceptObject));
end else
SysUtils.ShowException(ExceptObject, ExceptAddr);
}
end;
procedure TJDApplication.HandleMessage;
var
Msg: TMsg;
begin
if not ProcessMessage(Msg) then begin
//Idle(Msg);
end;
end;
function TJDApplication.ProcessMessage(var Msg: TMsg): Boolean;
var
Handled: Boolean;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message <> WM_QUIT then begin
Handled := False;
if Assigned(FOnMessage) then FOnMessage(Msg, Handled);
//if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and
//not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end else begin
fTerminated:= True;
end;
end;
end;
procedure TJDApplication.ProcessMessages;
var
Msg: TMsg;
begin
while ProcessMessage(Msg) do {loop};
end;
procedure TJDApplication.Initialize;
begin
if InitProc <> nil then TProcedure(InitProc);
end;
procedure TJDApplication.Run;
begin {
fRunning := True;
try
AddExitProc(DoneApplication);
if FMainForm <> nil then
begin
case CmdShow of
SW_SHOWMINNOACTIVE: FMainForm.FWindowState := wsMinimized;
SW_SHOWMAXIMIZED: MainForm.WindowState := wsMaximized;
end;
if FShowMainForm then
if FMainForm.FWindowState = wsMinimized then
Minimize else
FMainForm.Visible := True;
repeat
try
HandleMessage;
except
HandleException(Self);
end;
until Terminated;
end;
finally
FRunning := False;
end;
}
fRunning:= True;
try
AddExitProc(DoneApplication);
if fMainForm <> nil then begin
fHandle:= fMainForm.Handle;
if fShowMainForm then begin
fMainForm.Show;
end;
fThread.Start;
repeat
try
HandleMessage;
//--- THREAD HANDLING MESSAGES ---
except
HandleException(Self);
end;
until fTerminated;
end else begin
//Main form is nil - can not run
end;
finally
fRunning:= False;
fTerminated:= True;
end;
end;
procedure TJDApplication.Terminate;
begin
fTerminated:= True;
try
fThread.Stop;
except
end;
if CallTerminateProcs then PostQuitMessage(0);
end;
procedure TJDApplication.ThreadTerminated(Sender: TObject);
begin
//Free objects
end;
procedure TJDApplication.ThreadSync(Sender: TObject);
var
Msg: TMsg;
begin
if not ProcessMessage(Msg) then begin
//Idle(Msg);
end;
end;
{ TJDApplicationThread }
constructor TJDApplicationThread.Create(AOwner: TJDApplication);
begin
inherited Create(True);
fOwner:= AOwner;
end;
destructor TJDApplicationThread.Destroy;
begin
inherited;
end;
procedure TJDApplicationThread.DoSync;
begin
Self.fOwner.ThreadSync(Self);
// if assigned(fOnSync) then fOnSync(Self);
end;
procedure TJDApplicationThread.Execute;
var
ST: Integer;
begin
ST:= 5;
fStop:= False;
while (not Terminated) and (not fStop) do begin
//----- BEGIN -----
Synchronize(DoSync);
//----- END -----
//Sleep(1000 * ST);
end;
end;
procedure TJDApplicationThread.Start;
begin
fStop:= False;
Resume;
end;
procedure TJDApplicationThread.Stop;
begin
fStop:= True;
Suspend;
end;
initialization
JDApplication:= TJDApplication.Create(nil);
finalization
if assigned(JDApplication) then begin
JDApplication.Free;
JDApplication:= nil;
end;
end.
这是一个使用它的应用程序:
program Win7FormTestD7;
uses
Forms,
W7Form1 in 'W7Form1.pas' {Win7Form1},
JDForms in 'JDForms.pas';
begin
JDApplication.Initialize;
JDApplication.CreateForm(TWin7Form1, Win7Form1);
JDApplication.Run;
end.
表单“W7Form1”只是一个普通表单,上面有几个随机控件可供测试。
这里的用户不应该问我为什么要这样做,我有我的理由。我通过实践来学习,而不是通过某人向我展示或通过阅读某本书或找到一堆我不知道其工作原理的代码来学习。这是我更好地学习应用程序工作原理并能够扩展我在该领域的知识的一种方式,以便能够在未来构建更复杂的应用程序。
最佳答案
请记住,TCustomForm
没有 TJDApplication
类的概念,它仅适用于 Forms.TApplication
类。确保当 Forms.TApplication.Termulated
属性设置为 True 时,您的 TJDApplication.Run()
方法正在退出。
关于delphi - 创建替代 TApplication 进行实验?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373398/
我正在测试 SQL,但我陷入了一个查询。这是一个无用的查询,但我想理解它。 select count(*), floor(rand()*2) as x from table_name group by
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
在我编写代码时,我经常喜欢查看代码库中特定区域的工作原理或某些表达式产生的结果。 大多数其他语言都提供了在运行时评估新的自定义表达式的工具。 Golang 似乎还没有提供这个功能,或者至少没有我使用过
1、kvm 简介 kernel-based virtual machine的简称,是一个开源的系统虚拟化模块,自linux 2.6.20之后集成在linux的各个主要发行版本中。它使用linux
我是中继新手,我正在尝试在并发模式下使用中继现代实验。我已经能够使用 Suspense 和 ErrorBoundary 很好地加载节点、边等。我现在正在处理用于创建和更新对象的表单。 我无法弄清楚如何
我正在参加在线软件安全类(class)。我正在尝试使用 shellcode 进行实验。我编写了一个易受攻击的服务器、一个注入(inject)程序、一个(可能已损坏的)shellcode,我将其转换为程
我最糟糕的噩梦是日期对象,所以我创建了一个 fiddle 来查看它是如何工作的,并尝试为以下格式的 date strings 找到解决方案 2015-10- 05T11:49:13.587Z 但要注意
我一直在研究 Accessibility Object Model API,我认为尝试在 Puppeteer 测试中使用它会很酷。 getComputedAccessibleNode 返回一个 pro
我目前正在概述 C++11 的新特性,由于目前不明原因,其中一些特性无法编译。我使用 gcc version 4.6.0 20100703 (experimental) (GCC) 所以根据 GNU
1.动态年龄判定规则 对象进入老年代的4个常见的时机: 1、 躲过15次gc,达到15岁高龄之后进入老年代; 2、 动态年龄判定规则,如果Survivor区域内年龄1+年龄2+年龄3+年龄n的对象
有没有办法停止ray.tune实验(例如使用 PBT)当明显过度拟合或一个指标长时间没有改善时? 最佳答案 现在,这在 Tune 中得到了很好的支持,https://github.com/ray-pr
我尝试在gcc 6.0的开发中实际使用新的c++ 1z功能。 如果我尝试这个小例子: #include #include namespace fs = std::experimental::fil
我想知道为什么我在服务器端运行实验时必须包含 JavaScript cxApi。此外,我可以通过 PHP 发送选定的实验和变体吗?或者可能通过在没有外部资源(如 cxApi)的情况下注入(inject
我正在开发一个使用 Firebase iOS SDK 的 iOS 应用,我正在尝试弄清楚如何访问用户已看到的所有 Firebase AB 实验以及用户参与的实验变体。 我查看了文档以及 Firebas
我用 python 2.7.7 运行了以下命令: import gc import memory_profiler print memory_profiler.memory_usage()[0] x
我在浏览器控制台中做了一个 JavaScript 实验 - 首先我创建了一个新对象 foo 如下 var foo = { bar: function() { return this.baz
据说“4916 个正面训练示例经过手工挑选对齐、归一化并缩放到 24x24 的基本分辨率。通过从 9500 张不包含人脸的图像中随机挑选子窗口来选择 10,000 个负面示例。”在论文“Paul Vi
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Can you write object oriented code in C? 嗨! 只是为了好玩,这两天
1. 程序的JVM参数示范 已知,平时系统运行创建的对象,除非是那种大对象,否则通常来说都是优先分配在新生代中的Eden区域的。 而且新生代还有另外两块Survivor区域,默认Eden区域占据新
在 上创建新实验DAGsHub 使用Git,".._metrics.csv"的格式应该是什么和 ".._params.yml"文件? 不幸的是,我在任何地方都找不到引用。 最佳答案 特尔;博士: 对于
我是一名优秀的程序员,十分优秀!