delphi 多线程 数据库


// 线程类
unit Unit2;

interface

uses
Classes;

type
TMyThread = class(TThread)
private
FUserName: string;
FPassWord: string;
FFlag: Boolean;
procedure GetUserName(const Value: string);
procedure GetPassWord(const Value: string);
{ Private declarations }
protected
procedure Execute; override;
public
property UserName: string read FUserName write GetUserName;
property PassWord: string read FPassWord write GetPassWord;
property MyFunRetVal: Boolean read FFlag default False;
function Myfun(FUserName, FPassWord: string): Boolean;
constructor Create(b: Boolean = True);
end;

implementation

function TMyThread.Myfun(FUserName, FPassWord: string): Boolean;
begin
Result := True; // 简单起见让它固定为True
end;

constructor TMyThread.Create(b: Boolean = True);
begin
inherited Create(b);
Self.FreeOnTerminate := True;
end;

procedure TMyThread.Execute;
begin
FFlag := Myfun(FUserName, FPassWord);
end;

procedure TMyThread.GetPassWord(const Value: string);
begin
FPassWord := Value;
end;

procedure TMyThread.GetUserName(const Value: string);
begin
FUserName := Value;
end;

end.

//---------------------------------------------


// 调用单元
unit Unit1;

interface

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

type
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
public
procedure MyOnTerminate(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses Unit2; // 线程类定义在unit2单元

var
retVal: Boolean; // 线程"返回值"

procedure TForm1.btn1Click(Sender: TObject);
var
myThread: TMyThread;
begin
myThread := TMyThread.Create; // 创建线程并挂起
myThread.UserName := 'sololie'; // 传参
myThread.PassWord := '撸过更健康';
myThread.OnTerminate := MyOnTerminate; // 线程结束的回调函数
myThread.Resume; // 开始执行线程
end;

procedure TForm1.MyOnTerminate(Sender: TObject);
begin
retVal := (Sender as TMyThread).MyFunRetVal;
if retVal then
ShowMessage('线程执行函数返回 True')
else
ShowMessage('线程执行函数返回 False');
end;

end.

 

转载于:https://www.cnblogs.com/yuyang2100/p/4791794.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值