'工作,请等待'屏幕与线程? [英] 'working, please wait' screen with thread?

查看:190
本文介绍了'工作,请等待'屏幕与线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许,这对你来说很容易,但是我正在努力研究一个用TadSISearch组件查询adsi的项目(用于教育目的)几天。在TADSISearch正在搜索Active Directory时,我正试图在Form2上显示一个工作,请稍候...启动画面,并在Form2上创建一个动画GIF。虽然我根据我尝试了所有的可能性,但我不能成功。我尝试在一个线程中使用TADSISearch,但线程在ADSIsearch完成之前终止。我认为TADSISearch不是线程安全的。你怎么看?另外,我创建了Form2的另一种方式是使用线程进行更新,但是当主窗体进行adsi搜索时,动画GIF正在停止。你能说些什么?如何在ADSISearch正在运行并保持主窗体响应时,请等待屏幕。 Application.ProcessMessages或计时器也不是一种方式。非常感谢阅读和答案。

解决方案

图形用户界面应该由主线程更新。您应该将搜索代码放在一个单独的线程中,而当搜索线程正在工作时,您的主线程可以显示动画以及请等待消息。



您的搜索线程可以通过任何可用的同步技术完成搜索时通知主线程。最简单的方法是在线程类中定义一个方法,该方法在用户界面中停止动画,并在搜索器线程的Execute方法结束时将该方法传递给Synchronize。



你的搜索者线程代码将是这样的:

 键入
TMyThread = class(TThread)
private
procedure NotifyEndOfThread;
protected
procedure Execute;覆盖
结束

实现

使用MainFormUnit;

程序TMyThread.NotifyEndOfThread;
begin
MainForm.ShowAnimation:= False;
结束

程序TMyThread.Execute;
begin
try
{在此添加您的搜索代码}
finally
同步(NotifyEndOfThread);
结束
结束

你的主线程的代码将是这样的:

  TMainForm = class(TForm)
...
private
FShowAnimation:Boolean;
程序SetShowAnimation(Value:Boolean);
public
属性ShowAnimation:Boolean读取FShowAnimation写入SetShowAnimation;
结束

程序TMainForm.SetShowAnimation(Value:Boolean);
begin
FShowAnimation:= Value;
如果FShowAnimation然后
{添加动画代码在这里}
else
{停止动画}
结束;


Perhaps, it is very easy for you, but I am hard working on a project (for educational purposes) that is querying adsi with TADSISearch component, for several days. I'm trying to show a 'Working, Please wait..' splash screen with a man worker animated gif on Form2 while TADSISearch is searching the Active Directory. Although i tried every possibilities according to me, but i couldn't succeed. I tried to use TADSISearch in a thread, but thread is terminating before ADSIsearch finishes. I think TADSISearch is not thread safe. What do you think? Also, another way that I created Form2 and used a thread for updating it but the animated gif is stopping while main form gone adsi searching. What can you say about these? How can i make a please wait screen while ADSISearch is working and keep main form responding. Application.ProcessMessages or timer is not a way too. Thanks a lot for reading and answers.

解决方案

The graphical user interface should be updated by the main thread. You should put your search code into a separate thread, and while the searcher thread is working, your main thread can show the animation along with "Please wait" message.

Your searcher thread can notify the main thread when search is done by any of the available synchronization techniques. The simplest one is to define a method in your thread class which stops the animation in user interface, and pass that method to Synchronize at the end of Execute method of your searcher thread.

Your searcher thread code will be something like this:

type
  TMyThread = class(TThread)
  private
    procedure NotifyEndOfThread;
  protected
    procedure Execute; override;
  end;

implementation

uses MainFormUnit;

procedure TMyThread.NotifyEndOfThread;
begin
  MainForm.ShowAnimation := False;
end;

procedure TMyThread.Execute;
begin
  try
    {Add your search code here}
  finally
    Synchronize(NotifyEndOfThread);
  end;
end;

And your main thread's code will be like this:

TMainForm = class(TForm)
...
private 
  FShowAnimation : Boolean;
  procedure SetShowAnimation(Value: Boolean);
public
  property ShowAnimation : Boolean read FShowAnimation write SetShowAnimation;
end;

procedure TMainForm.SetShowAnimation(Value: Boolean);
begin
  FShowAnimation := Value;
  if FShowAnimation then
    {Add animation code here}
  else
    {Stop animation}
end;

这篇关于'工作,请等待'屏幕与线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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