如何从第二个线程更新进度指标? [英] How to update progress indicator from a second thread?

查看:175
本文介绍了如何从第二个线程更新进度指标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有进度指标的主窗体。
在数据模型中,我有十个数据集,每个都有一个定义的OnBeforeOpen事件。



我想通过主窗体中的进度条显示开放数据集进度的百分比。



由于我完全是多线程编程的新手,有人可以给我一些建议吗?



非常感谢

解决方案

从线程发布到主线程的消息,并更新进度条,或使用TThread.Queue方法在主线程的上下文中执行一些代码。

  unit Unit12; 

接口

使用
Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,
Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,Vcl.ComCtrls;

const
WM_UPDATE_PB = WM_USER;

type
TForm12 = class(TForm)
ProgressBar1:TProgressBar;
ProgressBar2:TProgressBar;
Button1:TButton;
procedure Button1Click(Sender:TObject);
private
public
procedure WMUpdatePB(var msg:TMessage);消息WM_UPDATE_PB;
结束

var
Form12:TForm12;

执行

{$ R * .dfm}

程序UpdateFromThreadViaMessage;
var
i:integer;
开始
为i:= 1到100 do begin
睡眠(20);
PostMessage(Form12.Handle,WM_UPDATE_PB,i,0);
结束
结束

程序UpdateFromThreadViaQueue;
var
i:integer;
开始
为i:= 1到100 do begin
睡眠(20);
TThread.Queue(nil,
procedure begin
Form12.ProgressBar2.Position:= i;
end);
结束
结束

procedure TForm12.Button1Click(Sender:TObject);
begin
TThread.CreateAnonymousThread(UpdateFromThreadViaMessage).Start;
TThread.CreateAnonymousThread(UpdateFromThreadViaQueue).Start;
结束

程序TForm12.WMUpdatePB(var msg:TMessage);
begin
ProgressBar1.Position:= msg.WParam;
结束

结束。


I have a main form with a progress indicator on it. In the datamodule I've ten datasets, each of them has an OnBeforeOpen event defined.

I would like to show through the progress bar in the main form a percentage of progress of the opened datasets.

Since I'm completely new to multithreading programming, can someone please give me some advice?

Thank you very much

解决方案

Either post a message from the thread to the main thread and update the progress bar from there or use the TThread.Queue method to execute some code in the context of the main thread.

unit Unit12;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;

const
  WM_UPDATE_PB = WM_USER;

type
  TForm12 = class(TForm)
    ProgressBar1: TProgressBar;
    ProgressBar2: TProgressBar;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
    procedure WMUpdatePB(var msg: TMessage); message WM_UPDATE_PB;
  end;

var
  Form12: TForm12;

implementation

{$R *.dfm}

procedure UpdateFromThreadViaMessage;
var
  i: integer;
begin
  for i := 1 to 100 do begin
    Sleep(20);
    PostMessage(Form12.Handle, WM_UPDATE_PB, i, 0);
  end;
end;

procedure UpdateFromThreadViaQueue;
var
  i: integer;
begin
  for i := 1 to 100 do begin
    Sleep(20);
    TThread.Queue(nil,
      procedure begin
        Form12.ProgressBar2.Position := i;
      end);
  end;
end;

procedure TForm12.Button1Click(Sender: TObject);
begin
  TThread.CreateAnonymousThread(UpdateFromThreadViaMessage).Start;
  TThread.CreateAnonymousThread(UpdateFromThreadViaQueue).Start;
end;

procedure TForm12.WMUpdatePB(var msg: TMessage);
begin
  ProgressBar1.Position := msg.WParam;
end;

end.

这篇关于如何从第二个线程更新进度指标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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