英文:
Delphi 2007 Updating a console application from a datamodule using TIdHTTPServer within httpServerCommandGet
问题
以下是翻译好的内容:
我们有以下类,我们希望在数据模块上的过程中使用 Indy 来更新控制台。
TLog = class(TIdNotify)
protected
FMsg: string;
procedure DoNotify; override;
public
class procedure LogMsg(const AMsg: string);
end;
procedure TLog.DoNotify;
begin
writeln(FMsg); //这可能完全错误
end;
class procedure TLog.LogMsg(const AMsg: string);
begin
with TLog.Create do
try
FMsg := AMsg;
Notify;
except
Free;
raise;
end;
end;
我们希望像这样发送消息:
procedure THTTPServer.httpServerCommandGet(
AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
begin
TLog.LogMsg('test');
end;
这不起作用。
有没有人有一个简单的工作版本的控制台应用程序,可以在数据模块和控制台上同步线程?老实说,我甚至不确定是否这是问题。
我们在试图理解文档方面遇到了困难。
我们曾简要查看过这段代码,但对我们来说没有用。
<details>
<summary>英文:</summary>
We have the following class we want to use to update the console from within a procedure on a datamodule using Indy.
TLog = class(TIdNotify)
protected
FMsg: string;
procedure DoNotify; override;
public
class procedure LogMsg(const AMsg : string);
end;
procedure TLog.DoNotify;
begin
writeln(fMsg); //this is probably completely wrong
end;
class procedure TLog.LogMsg(const AMsg: string);
begin
with TLog.Create do
try
FMsg := AMsg;
Notify;
except
Free;
raise;
end;
end;
We want to then send messages back like this
procedure THTTPServer.httpServerCommandGet(
AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
begin
TLog.LogMsg('test');
This is not working.
Does anyone have a simple working version of a console app which is synchronized threads on the datamodule and the console? I'm not even sure if that is the problem tbh
We are struggling to make sense of the documentation.
[Delphi CheckSynchronize](https://i.stack.imgur.com/LmTAI.png)
We had a brief look at this code, but it didn't work for us
https://stackoverflow.com/questions/2591774/how-to-create-a-console-application-that-does-not-terminate
</details>
# 答案1
**得分**: 0
你的 `TLog` 代码没问题。问题在于,默认情况下,控制台应用程序根本没有消息循环,因此没有运行以自动处理 `TIdNotify` 请求,就像在 GUI 应用程序中一样。这就是为什么你的代码在控制台应用程序中不起作用的原因。
你的控制台应用程序的主入口点需要定期调用 RTL 的 `CheckSynchronize()` 函数。你可以使用 RTL 的 `SyncEvent` 句柄来检测是否有待处理的请求等待处理。
尝试像这样做:
```delphi
program MyApp;
{$APPTYPE CONSOLE}
uses
..., Classes, Windows;
...
begin
...
while not SomeCondition do
begin
...
if WaitForSingleObject(SyncEvent, 100) = WAIT_OBJECT_0 then
CheckSynchronize;
...
end;
...
end.
英文:
Your TLog
code is fine. The problem is, by default, a console application simply does not have a message loop, so there is nothing running to process the TIdNotify
requests automatically, like in a GUI app. That is why your code is not working in a console app.
Your console app's main entry point needs to call the RTL's CheckSynchronize()
function periodically. You can use the RTL's SyncEvent
handle to detect when there are pending requests waiting to be processed.
Try something like this:
program MyApp;
{$APPTYPE CONSOLE}
uses
..., Classes, Windows;
...
begin
...
while not SomeCondition do
begin
...
if WaitForSingleObject(SyncEvent, 100) = WAIT_OBJECT_0 then
CheckSynchronize;
...
end;
...
end.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论