在 TWebModule Handler 中生成新的线程/进程

发布于 2024-12-21 07:35:51 字数 912 浏览 3 评论 0原文

我有一个在 Apache 上运行的基于 Delphi TWebModule ISAPI 的项目。我的一个事件处理程序包含可能需要几分钟才能处理的逻辑。我想生成一个单独的进程/线程来执行逻辑并立即将 html 返回到浏览器。 html 将具有 AJAX 客户端调用来获取流程进度的定期更新。

我尝试过使用TThread,但发现它等待执行代码结束才返回。

示例:

  procedure Tmainweb.DoLongProcess(Sender: TObject; Request: TWebRequest;
    Response: TWebResponse; var Handled: Boolean);
  var
    ProcessThread: TProcessThread;
  begin
    ProcessThread := TProcessThread.Create(True);
    ProcessThread.Execute;
    Handled := True;
    Response.Content := '<html><body>Processing - would also include ajax stuff to get periodic updates</body></html>
  end;

TProcessThread 是我的处理线程,可能需要几分钟才能完成。当我运行这个应用程序时,我认为控制将在 ProcessThread.Execute 之后立即继续。但事实并非如此。相反,它会等待执行过程中的代码完成。

我怎样才能做到这一点?如何生成异步进程以使浏览器不处于等待状态?

I have a Delphi TWebModule ISAPI based project running on Apache. One of my event handlers contains logic that can take minutes to process. I would like to spawn a separate process/thread to perform the logic and return html immediately back to the browser. The html would have AJAX client side calls to get periodic updates of the process progress.

I have tried using TThread, but find it waits for the Execute code to end before returning.

Example:

  procedure Tmainweb.DoLongProcess(Sender: TObject; Request: TWebRequest;
    Response: TWebResponse; var Handled: Boolean);
  var
    ProcessThread: TProcessThread;
  begin
    ProcessThread := TProcessThread.Create(True);
    ProcessThread.Execute;
    Handled := True;
    Response.Content := '<html><body>Processing - would also include ajax stuff to get periodic updates</body></html>
  end;

TProcessThread is my processing thread which may take minutes to complete. When I run this application I thought control would continue immeidately after ProcessThread.Execute. But it does not. Instead it waits for the code in the Execute procedure to complete.

How can I accomplish this? How to I spawn a asynchronous process so that the browser is not in a wait state?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2024-12-28 07:35:51

只是旋转达里安的答案。
这是一个回答您问题的示例:

type
  TProcessThread = class(TThread)
  protected 
    procedure Execute; override;
  public
    constructor Create;
  end;

constructor TProcessThread.Create;
begin
 inherited Create( false);
 Self.FreeOnTerminate := true;
end;

procedure TProcessThread.Execute;
begin
  while not Self.Terminated do begin
    {- Do some heavy work }
  end;
  {- free by myself at last ! }
end;

-

// In your TmainWeb.DoLongProcess
ProcessThread := TProcessThread.Create; // Thread will free itself when ready.
Handled := True;

Just spinning on Darian's answer.
Here's an example which answers your question :

type
  TProcessThread = class(TThread)
  protected 
    procedure Execute; override;
  public
    constructor Create;
  end;

constructor TProcessThread.Create;
begin
 inherited Create( false);
 Self.FreeOnTerminate := true;
end;

procedure TProcessThread.Execute;
begin
  while not Self.Terminated do begin
    {- Do some heavy work }
  end;
  {- free by myself at last ! }
end;

-

// In your TmainWeb.DoLongProcess
ProcessThread := TProcessThread.Create; // Thread will free itself when ready.
Handled := True;
长安忆 2024-12-28 07:35:51

没有足够的信息来给出正确的答案,但我假设 TProcessThread 以某种方式继承自 TThread。如果是这样,那么您创建线程然后启动它。 execute方法会在子线程中调用,不能直接调用。

ProcessThread.FreeOnTerminate := True
ProcessThread.Start() // Later versions of Delphi
//or  ProcessThread.Resume;  in earlier versions of Delphi to start a suspended thread

There's not quite enough info to give a correct answer, but I am assuming TProcessThread inherits from TThread somehow. If so, then you create the thread and then Start it. The execute method will be called within the child thread and is not to be called directly.

ProcessThread.FreeOnTerminate := True
ProcessThread.Start() // Later versions of Delphi
//or  ProcessThread.Resume;  in earlier versions of Delphi to start a suspended thread
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文