Delphi:如何在本地创建和使用Thread? [英] Delphi : How to create and use Thread locally?

查看:85
本文介绍了Delphi:如何在本地创建和使用Thread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库位于VPS中,我应该从表中查询一些

My database is in a VPS and I should get some query from my tables

由于从服务器获取查询需要很长时间(取决于Internet速度!),我想使用线程来获取查询

Because of getting query from server taking long time ( depending on Internet speed ! ) , I want to use threads to get queries

现在我创建一个线程并获取查询,然后通过发送和处理消息将结果发送到表单中

Now I create a thread and get query and then send result to my forms with sending and handling messages

我想知道是否可以在本地创建和使用线程?!?

I want to know is it possible to create and use a thread locally ?!?

我的意思是:

procedure Requery;
var
 ...
begin
 Create Thread;
 ...

 Pass my Query Component to Thread
 ...

 Getting Query in Thread;
 ...

 Terminate and Free Thread
 ...

 Do next jobs with Query;
 ...

end;

主要部分是最后一部分(做下一个工作...),我不想使用查询导致出现消息处理程序,我想在相同的过程中以及线程作业之后使用它们

The main part is last part ( Do next jobs ... ) , I dont want to use query result in a message handler and I want to use them in the same procedure and after thread job

有可能吗?!

我认为这对于Delphi TThread类是不可能的,我应该使用其他线程技术...

I think this is not possible with Delphi TThread class and I should use other threading techniques ...


  • 我使用Delphi XE6

推荐答案

您所描述的并不是最佳使用线程。调用代码被阻塞,直到线程完成为止。这就完全消除了并行运行代码的使用。您可以直接直接执行查询:

What you describe is not the best use of a thread. The calling code is blocked until the thread is finished. That negates the use of running code in parallel at all. You could just perform the query directly instead:

procedure Requery;
var
  ...
begin
  ...
  // run query
  // do next jobs with query
  ...
end;

话虽如此,由于您使用的是XE6,因此可以创建本地使用 TThread.CreateAnonymousThread( ) 方法,指定匿名过程捕获您要使用的变量,例如:

That being said, since you are using XE6, you can create a "local" thread by using the TThread.CreateAnonymousThread() method, specifying an anonymous procedure that "captures" the variables you want it to work with, eg:

procedure Requery;
var
  Event: TEvent;
  H: THandle;
begin
  Event := TEvent.Create;
  try
    TThread.CreateAnonymousThread(
      procedure
      begin
        try
          // run query in thread
        finally
          Event.SetEvent;
        end;
      end
    ).Start;
    H := Event.Handle;
    while MsgWaitForMultipleObjects(1, H, False, INFINITE, QS_ALLINPUT) = (WAIT_OBJECT_0+1) do
      Application.ProcessMessages;
  finally
    Event.Free;
  end;

  // Do next jobs with query
  ...
end;

或者:

procedure Requery;
var
  Thread: TThread;
  H: THandle;
begin
  Thread := TThread.CreateAnonymousThread(
    procedure
    begin
      // run query in thread
    end
  );
  try
    Thread.FreeOnTerminate := False;
    H := Thread.Handle;
    Thread.Start;
    while MsgWaitForMultipleObjects(1, H, False, INFINITE, QS_ALLINPUT) = (WAIT_OBJECT_0+1) do
      Application.ProcessMessages;
  finally
    Thread.Free;
  end;

  // Do next jobs with query
  ...
end;

但是,线程在您执行其他操作时让它在后台运行,然后在线程已完成工作。例如:

However, threading is more useful when you let it run in the background while you do other things and then you act when the thread has finished its work. For example:

procedure TMyForm.Requery;
var
  Thread: TThread;
begin
  Thread := TThread.CreateAnonymousThread(
    procedure
    begin
      // run query in thread
    end
  );
  Thread.OnTerminate := QueryFinished;
  Thread.Start;
end;

procedure TMyForm.QueryFinished(Sender: TObject);
begin
  if TThread(Sender).FatalException <> nil then
  begin
    // something went wrong
    Exit;
  end;
  // Do next jobs with query
end;

这篇关于Delphi:如何在本地创建和使用Thread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆