小弟又来跪求正解!(100求正解) ( 积分: 100 )

  • 主题发起人 主题发起人 cdesign
  • 开始时间 开始时间
C

cdesign

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟太笨,可能是个很简单的问题,但是一直解决不了。
问题描述:
当点击一个按钮的时候,执行一个查询或者其他什么,这个时候需要显示一个数据交换的动画提示。但是怎么老是等查询结束才能显示动画,否则不是不动就是不现实。
是不是要用到多线程?下面是一个模拟的效果,如果它能for循环之前正确显示动画,应该就没问题了:
代码:
type
  //定义一个线程类
  TsyncThread = class(TThread)
  protected
    procedure execute;
override;
  end;

  TfrmNone = class(TForm)
    ListBox2: TListBox;
    ListBox1: TListBox;
    btnExecute: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure btnExecuteClick(Sender: TObject);
  private
    //线程实例
    Thread1, Thread2: TSyncThread;
    //自定义过程,在线程结束时调用
    procedure Thread1Done(sender: TObject);
    procedure Thread2Done(sender: TObject);
    { Private declarations }
  public
    { Public declarations }
  end;
const
  MaxSize = 100;
var
  frmNone: TfrmNone;
  //数据全局区
  GlobalData: array[1..MaxSize] of integer;
  IncNum: Integer = 0;
implementation
{$R *.dfm}
//线程执行主体
procedure TSyncThread.execute;
var
  i: integer;
begin
  FreeOnTerminate := True;
  for i := 1 to Maxsizedo
  begin
    Inc(IncNum);
    GlobalData[i] := IncNum;
    sleep(1);
  end;
end;

//创建线程实例
procedure TfrmNone.btnExecuteClick(Sender: TObject);
begin
  Thread1 := TSyncThread.Create(false);
  Thread1.OnTerminate := Thread1Done;
  Thread2 := TSyncThread.Create(false);
  Thread2.OnTerminate := Thread2Done;
end;

//当线程1执行完后,将GlobalData数据显示出来
procedure TfrmNone.Thread1Done(sender: TObject);
var
  i: integer;
begin
  ListBox1.Items.Clear;
  for i := 1 to MaxSizedo
    Listbox1.Items.Add(intToStr(GlobalData[i]));
end;

//当线程2执行完后执行 ,将GlobalData数据显示出来
procedure TfrmNone.Thread2Done(sender: TObject);
var
  i: integer;
begin
  ListBox2.Items.Clear;
  for i := 1 to MaxSizedo
    Listbox2.Items.Add(intToStr(GlobalData[i]));
end;

end.
 
小弟太笨,可能是个很简单的问题,但是一直解决不了。
问题描述:
当点击一个按钮的时候,执行一个查询或者其他什么,这个时候需要显示一个数据交换的动画提示。但是怎么老是等查询结束才能显示动画,否则不是不动就是不现实。
是不是要用到多线程?下面是一个模拟的效果,如果它能for循环之前正确显示动画,应该就没问题了:
代码:
type
  //定义一个线程类
  TsyncThread = class(TThread)
  protected
    procedure execute;
override;
  end;

  TfrmNone = class(TForm)
    ListBox2: TListBox;
    ListBox1: TListBox;
    btnExecute: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure btnExecuteClick(Sender: TObject);
  private
    //线程实例
    Thread1, Thread2: TSyncThread;
    //自定义过程,在线程结束时调用
    procedure Thread1Done(sender: TObject);
    procedure Thread2Done(sender: TObject);
    { Private declarations }
  public
    { Public declarations }
  end;
const
  MaxSize = 100;
var
  frmNone: TfrmNone;
  //数据全局区
  GlobalData: array[1..MaxSize] of integer;
  IncNum: Integer = 0;
implementation
{$R *.dfm}
//线程执行主体
procedure TSyncThread.execute;
var
  i: integer;
begin
  FreeOnTerminate := True;
  for i := 1 to Maxsizedo
  begin
    Inc(IncNum);
    GlobalData[i] := IncNum;
    sleep(1);
  end;
end;

//创建线程实例
procedure TfrmNone.btnExecuteClick(Sender: TObject);
begin
  Thread1 := TSyncThread.Create(false);
  Thread1.OnTerminate := Thread1Done;
  Thread2 := TSyncThread.Create(false);
  Thread2.OnTerminate := Thread2Done;
end;

//当线程1执行完后,将GlobalData数据显示出来
procedure TfrmNone.Thread1Done(sender: TObject);
var
  i: integer;
begin
  ListBox1.Items.Clear;
  for i := 1 to MaxSizedo
    Listbox1.Items.Add(intToStr(GlobalData[i]));
end;

//当线程2执行完后执行 ,将GlobalData数据显示出来
procedure TfrmNone.Thread2Done(sender: TObject);
var
  i: integer;
begin
  ListBox2.Items.Clear;
  for i := 1 to MaxSizedo
    Listbox2.Items.Add(intToStr(GlobalData[i]));
end;

end.
 
TsyncThread = class(TThread)
private
FDest: TStrings;
FIndex: Integer;
procedure Step;
protected
procedure execute;
override;
public
constructor Create(CreateSuspended: Boolean;
Dest: TStrings);
end;
constructor TsyncThread.Create(CreateSuspended: Boolean;
Dest: TStrings);
begin
FDest := Dest;
inherited Create(CreateSuspended);
end;
procedure TsyncThread.Step;
begin
Inc(IncNum);
GlobalData := IncNum;
FDest.Add(IntToStr(GlobalData[FIndex]));
end;
//线程执行主体
procedure TSyncThread.execute;
var
i: integer;
begin
FreeOnTerminate := True;
for i := 1 to Maxsizedo
begin
FIndex := i;
//Inc(IncNum);
//GlobalData := IncNum;
不能这么多线程直接更改全局变量哦
Synchronize(Step);
sleep(1);
end;
end;
 
试一下这个方法看看???
有的时候後,我们需要一个Modal窗体,但是同时需要程序继续运行进行其他的控制,那么该这么办呢?
利用DisableTaskWindows和 EnableTaskWindows 可以达到这一效果。
  下面是演示实现程序:
procedure TForm1.ShowProgressDlg;
var
WindowList: Pointer;
begin

{除了Form2外使所有窗口无效}
 WindowList := DisableTaskWindows(Form2.Handle);
 try
  Form2.Show;
  {循环执行任务}
  Form2.ProgressBar1.Position :=
  Form2.ProgressBar1.Position + 1;
  {循环结束}
 finally
  {重新使所有窗口有效}
  EnableTaskWindows(WindowList);
  Form2.Close;
 end;

end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
612
import
I
后退
顶部