delphi 多线程带参数

baidu_31461571 2015-09-21 09:37:03
如何将创建带参数的多线程,麻烦能将TForm1.threadtest改变成一个带参数的多线程,利用tthread类


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ProgressBar1: TProgressBar;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure threadtest(strtest:string);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.threadtest(strtest:string);
begin
showmessage(strtest);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
threadtest('str');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
I: Integer;
begin
for I := 0 to ProgressBar1.Max do
begin
Label1.Caption := '主线程运行中';
ProgressBar1.Position := i;
Application.ProcessMessages;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
progressbar1.Max := 1000000;
end;

end.
...全文
595 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
SupermanTm 2015-12-10
  • 打赏
  • 举报
回复
PostThreadMessage
SupermanTm 2015-12-10
  • 打赏
  • 举报
回复
或者用信号量 SetEvent 线程循环中用 WaitforMultiEvent...
smlabc1234 2015-09-22
  • 打赏
  • 举报
回复
procedure TThread1.Execute; // 线程执行 var I: Integer; begin i:=0; ThreadEnd:=False; FreeOnTerminate:=true; Repeat i:=i+1; Form1.ProgressBar1.Position := i; Application.ProcessMessages; until (I>=Form1.ProgressBar1.Max) or ThreadEnd; end; @lyhoo163 如果非要用线程类,在控制窗口控件时,需要用线程类的同步方法:Synchronize。
Mr Dang 2015-09-21
  • 打赏
  • 举报
回复
第一个办法是在加入到线程类析构函数中,有局限性,适合创建线程不挂起线程马上获取你传递的参数才能正常运行的情况,第二种是在线程类的publish域添加property 。使用时,创建线程对象后,将参数传入到property中去。提示:不要使线程去挂起来同步线程,应该使用同步技术去同步线程。
zwq908460961 2015-09-21
  • 打赏
  • 举报
回复
这是一个值得思考的问题。。。。
zwq908460961 2015-09-21
  • 打赏
  • 举报
回复
似乎并不怎么懂啊
baidu_31461571 2015-09-21
  • 打赏
  • 举报
回复
upupupupup
baidu_31461571 2015-09-21
  • 打赏
  • 举报
回复
初学delphi,实在不懂如何修改。。。
baidu_31461571 2015-09-21
  • 打赏
  • 举报
回复
跪求大神帮忙,这个问题十分纠结。。。。。。。。。。。。。。。。。。。。。。。
看那山瞧那水 2015-09-21
  • 打赏
  • 举报
回复
引用 6 楼 a295281315 的回复:
第一个办法是在加入到线程类析构函数中,有局限性,适合创建线程不挂起线程马上获取你传递的参数才能正常运行的情况,第二种是在线程类的publish域添加property 。使用时,创建线程对象后,将参数传入到property中去。提示:不要使线程去挂起来同步线程,应该使用同步技术去同步线程。
建议用第二种 线程类里声明个属性(参数),如果参数在线程运行中是不变的,刚创建线程的时候就设置属性值。如果参数在线程运行中要改变,则要改变参数时,暂停线程,然后设置属性值,再启动线程。 暂停可以用线程的waitfor函数
lyhoo163 2015-09-21
  • 打赏
  • 举报
回复
你的问题,提醒一下: 一、不要使用API的线程,直接使用Delphi年代的线程类Tthread非常方便。它已经封闭必要的方法。 二、在线程中,不要使用For循环,它不便中止。建议使用Repeat循环,设置一全局变量ThreadEnd,专用中途中止线程。 三、在procedure TThread1.Execute中,是线程中执行的代码,只要代码执行完毕,线程自动终止,释放占用内存。 下面是对你的代码进行修改的源码,仅供参考:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TThread1 = class(Tthread)
    protected
      procedure Execute;override;
  end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  ThreadEnd:boolean;

implementation

{$R *.dfm}

procedure TThread1.Execute;               // 线程执行
var
 I: Integer;
begin
  i:=0;
  ThreadEnd:=False;
  FreeOnTerminate:=true;
  Repeat
    i:=i+1;
    Form1.ProgressBar1.Position := i;
    Application.ProcessMessages;
  until (I>=Form1.ProgressBar1.Max) or  ThreadEnd;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  progressbar1.Max := 100000;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyThread:TThread1;
begin
  MyThread:=TThread1.Create(False);    // 创建线程并执行线程
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ThreadEnd:=True;                    // 中止线程
end;

end.
smlabc1234 2015-09-21
  • 打赏
  • 举报
回复
TThread类。。。害了多少人,用CreateThread做,简单实用,原版API,不加任何添加防腐剂。 相关API: CreateThread TerminateThread WaitForSingleObject InitializeCriticalSection DeleteCriticalSection EnterCriticalSection LeaveCriticalSection
  • 打赏
  • 举报
回复
TMyThread=class(TThread) FParam : string; constructor Create(AParam: string); end; constructor TMyThread.Create(AParam: string); begin FParam:=AParam; inherited Create(True); end;
baidu_31461571 2015-09-21
  • 打赏
  • 举报
回复
引用 6 楼 a295281315 的回复:
第一个办法是在加入到线程类析构函数中,有局限性,适合创建线程不挂起线程马上获取你传递的参数才能正常运行的情况,第二种是在线程类的publish域添加property 。使用时,创建线程对象后,将参数传入到property中去。提示:不要使线程去挂起来同步线程,应该使用同步技术去同步线程。
具体该怎么操作呢,刚学delphi,并不知道该怎么处理?

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧