delphi 多线程 数据库
2015-09-08 15:15
281 查看
// 线程类
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.
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.
相关文章推荐
- Delphi 2009
- Delphi引用COM自动生成TLB的方法
- Delphi旋转文字
- delphi2007控件安装多版本可以用多用户方法来解决
- Delphi 内嵌WebBrowser报错的问题
- delphi用mdb
- Delphi 读写其他进程内存
- Delphi的Randomize
- Delphi 类似实现多窗口显示任务栏上
- Delphi共用体
- DELPHI - How to use opendialog1 for choosing a folder? TOpenDialog, TFileOpenDialog
- Delphi中的匿名方法和闭包
- 咏南多层开发框架支持最新的DELPHI 10 SEATTLE
- How to add Leading Zeroes to a Number (Delphi Format)
- DELPHI安装第三方组件
- Delphi 10 Seattle,C++ Builder 10 Seattle,RAD Studio 10 Seattle 官方下载激活
- Delphi的字节对齐和简单string结构
- Delphi类里面的方法和类型定义不影响创建实例的大小
- C#的string有null的概念,但是Delphi却没有
- C#和Delphi跟踪的问题,和实际数据不一样