Z
zzmbeyond01
Unregistered / Unconfirmed
GUEST, unregistred user!
自己刚刚接触多线程,这是调试的一点感受:
这是delphi5开发人员指南中多线程的例子
按F7调试,发现为什么在create执行之后看不到execute执行,冥思苦想之后
在其execute的一些语句中设置断点,看到了execute执行情况。看到了两个线程执行
但小兄(半道转行)仍然有很多的不懂得问题,向各位专家请教,
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
procedure ThreadsDone(Sender: TObject);
end;
TFooThread = class(TThread)
protected
procedure Execute;
override;
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
const
MaxSize = 3;
var
NextNumber: Integer = 0;
do
neFlags: Integer = 0;
//记录线程数
GlobalArray: array[1..MaxSize] of Integer;
CS: TRTLCriticalSection;
function GetNextNumber: Integer;
begin
Result := NextNumber;
// return global var
inc(NextNumber);
// inc global var
end;
procedure TFooThread.Execute;
var
i: Integer;
begin
OnTerminate := MainForm.ThreadsDone;
// 设置断点。应当首先执行,为什么在最后才执行
EnterCriticalSection(CS);
//进入临界区 ,设置断点
for i := 1 to MaxSizedo
begin
GlobalArray := GetNextNumber;
// set array element
Sleep(5);
// let thread intertwine
end;
LeaveCriticalSection(CS);
//离开临界区 ,设置断点
end;
procedure TMainForm.ThreadsDone(Sender: TObject);
var
i: Integer;
begin
inc(DoneFlags);
ifdo
neFlags = 2 then
{设置一个断点}
begin
// make sure both threads finished
for i := 1 to MaxSizedo
{ fill listbox with array contents }
Listbox1.Items.Add(IntToStr(GlobalArray));
DeleteCriticalSection(CS);
end;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
InitializeCriticalSection(CS);
TFooThread.Create(False);
// create threads
TFooThread.Create(False);
end;
end.
我想问,OnTerminate事件是在一个线程执行完之后,就要执行的事件吗,他有什么用途
sleep函数,是分别执行这两个线程的时间片(我看帮助后,自己的理解,有错误请指正),
该函数在调试的时候,把我是有点搞糊涂了[][]
这是delphi5开发人员指南中多线程的例子
按F7调试,发现为什么在create执行之后看不到execute执行,冥思苦想之后
在其execute的一些语句中设置断点,看到了execute执行情况。看到了两个线程执行
但小兄(半道转行)仍然有很多的不懂得问题,向各位专家请教,
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
procedure ThreadsDone(Sender: TObject);
end;
TFooThread = class(TThread)
protected
procedure Execute;
override;
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
const
MaxSize = 3;
var
NextNumber: Integer = 0;
do
neFlags: Integer = 0;
//记录线程数
GlobalArray: array[1..MaxSize] of Integer;
CS: TRTLCriticalSection;
function GetNextNumber: Integer;
begin
Result := NextNumber;
// return global var
inc(NextNumber);
// inc global var
end;
procedure TFooThread.Execute;
var
i: Integer;
begin
OnTerminate := MainForm.ThreadsDone;
// 设置断点。应当首先执行,为什么在最后才执行
EnterCriticalSection(CS);
//进入临界区 ,设置断点
for i := 1 to MaxSizedo
begin
GlobalArray := GetNextNumber;
// set array element
Sleep(5);
// let thread intertwine
end;
LeaveCriticalSection(CS);
//离开临界区 ,设置断点
end;
procedure TMainForm.ThreadsDone(Sender: TObject);
var
i: Integer;
begin
inc(DoneFlags);
ifdo
neFlags = 2 then
{设置一个断点}
begin
// make sure both threads finished
for i := 1 to MaxSizedo
{ fill listbox with array contents }
Listbox1.Items.Add(IntToStr(GlobalArray));
DeleteCriticalSection(CS);
end;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
InitializeCriticalSection(CS);
TFooThread.Create(False);
// create threads
TFooThread.Create(False);
end;
end.
我想问,OnTerminate事件是在一个线程执行完之后,就要执行的事件吗,他有什么用途
sleep函数,是分别执行这两个线程的时间片(我看帮助后,自己的理解,有错误请指正),
该函数在调试的时候,把我是有点搞糊涂了[][]