新手遇到线程队列问题,求助 ( 积分: 100 )

  • 主题发起人 主题发起人 mh527
  • 开始时间 开始时间
M

mh527

Unregistered / Unconfirmed
GUEST, unregistred user!
不知道为什么一到 task:=tasklist.items;就提示
[error]wnit2.pas[125]:incompatible types:'TTaskRec' and 'pointer'
谢谢各位老大了
unit Unit2;
interface
uses
Classes,StdCtrls,Windows,SysUtils,wininet;
var
CS:TRTLCriticalSection;
//定义全局临界区
type
t1 = class(TThread)
private
TmpNum: integer;
tasklist:TList;//定义一个任务队列

procedure SetName;
procedure DataMemo;
protected
procedure Execute;
override;
public
constructor Create(Num: integer);
end;
var
tasklist:TList;//定义一个任务队列
type

TTaskRec = Record
ID :Integer;
// 任务唯一标示
RuningFlag:boolean;//正在执行标志
xxx:String;//需要执行的任务
end;

implementation
uses Unit1;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure t1.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{$IFDEF MSWINDOWS}
type
TThreadNameInfo = record
FType: LongWord;
// must be 0x1000
FName: PChar;
// pointer to name (in user address space)
FThreadID: LongWord;
// thread ID (-1 indicates caller thread)
FFlags: LongWord;
// reserved for future use, must be zero
end;
{$ENDIF}
{ t1 }
procedure t1.SetName;
{$IFDEF MSWINDOWS}
var
ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
ThreadNameInfo.FType := $1000;
ThreadNameInfo.FName := 't1';
ThreadNameInfo.FThreadID := $FFFFFFFF;
ThreadNameInfo.FFlags := 0;
try
RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
except
end;
{$ENDIF}
end;


constructor t1.Create(Num: integer);
begin
TmpNum := Num;
// 传递参数
FreeOnTerminate := True;
// 自动删除
InitializeCriticalSection(CS);
//初始化临界区
inherited Create(False);
// 直接运行
end;

procedure T1.DataMemo;
begin
while (form1.ListBox1.Items.Count > 0)do
begin
form1.ListBox2.Items.Add(form1.ListBox1.Items[0]+'--'+inttostr(TmpNum));
form1.ListBox1.Items.Delete(0);
form1.statusbar1.Panels[1].Text:=inttostr(form1.listbox1.Items.Count)+'--'+inttostr(form1.listbox2.Items.Count);

end;
end;

procedure t1.Execute;
var
task:TTaskRec;
i:integer;
begin
SetName;
while not Terminateddo
begin
if tasklist.Count<=0 then
begin
//没有任务时等待
Sleep(10);
Continue;
end;
For i:=0 to tasklist.Count-1do
begin
task:=tasklist.items;
if task.Runing then
begin
//已经在执行,则返回继续判断下一个
Sleep(10);
continue;
end;
task.Runing:=true;
EnterCriticalSection(cs);
//进入临界区
Synchronize(DataMemo);
// 同步
LeaveCriticalSection(CS);
//执行完成后从队列中删除,要注意线程保护这个任务的删除,添加操作
tasklist.delete;
break;//跳出这个任务循环
end;
end;


end;

end.
 
不知道为什么一到 task:=tasklist.items;就提示
[error]wnit2.pas[125]:incompatible types:'TTaskRec' and 'pointer'
谢谢各位老大了
unit Unit2;
interface
uses
Classes,StdCtrls,Windows,SysUtils,wininet;
var
CS:TRTLCriticalSection;
//定义全局临界区
type
t1 = class(TThread)
private
TmpNum: integer;
tasklist:TList;//定义一个任务队列

procedure SetName;
procedure DataMemo;
protected
procedure Execute;
override;
public
constructor Create(Num: integer);
end;
var
tasklist:TList;//定义一个任务队列
type

TTaskRec = Record
ID :Integer;
// 任务唯一标示
RuningFlag:boolean;//正在执行标志
xxx:String;//需要执行的任务
end;

implementation
uses Unit1;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure t1.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{$IFDEF MSWINDOWS}
type
TThreadNameInfo = record
FType: LongWord;
// must be 0x1000
FName: PChar;
// pointer to name (in user address space)
FThreadID: LongWord;
// thread ID (-1 indicates caller thread)
FFlags: LongWord;
// reserved for future use, must be zero
end;
{$ENDIF}
{ t1 }
procedure t1.SetName;
{$IFDEF MSWINDOWS}
var
ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
ThreadNameInfo.FType := $1000;
ThreadNameInfo.FName := 't1';
ThreadNameInfo.FThreadID := $FFFFFFFF;
ThreadNameInfo.FFlags := 0;
try
RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
except
end;
{$ENDIF}
end;


constructor t1.Create(Num: integer);
begin
TmpNum := Num;
// 传递参数
FreeOnTerminate := True;
// 自动删除
InitializeCriticalSection(CS);
//初始化临界区
inherited Create(False);
// 直接运行
end;

procedure T1.DataMemo;
begin
while (form1.ListBox1.Items.Count > 0)do
begin
form1.ListBox2.Items.Add(form1.ListBox1.Items[0]+'--'+inttostr(TmpNum));
form1.ListBox1.Items.Delete(0);
form1.statusbar1.Panels[1].Text:=inttostr(form1.listbox1.Items.Count)+'--'+inttostr(form1.listbox2.Items.Count);

end;
end;

procedure t1.Execute;
var
task:TTaskRec;
i:integer;
begin
SetName;
while not Terminateddo
begin
if tasklist.Count<=0 then
begin
//没有任务时等待
Sleep(10);
Continue;
end;
For i:=0 to tasklist.Count-1do
begin
task:=tasklist.items;
if task.Runing then
begin
//已经在执行,则返回继续判断下一个
Sleep(10);
continue;
end;
task.Runing:=true;
EnterCriticalSection(cs);
//进入临界区
Synchronize(DataMemo);
// 同步
LeaveCriticalSection(CS);
//执行完成后从队列中删除,要注意线程保护这个任务的删除,添加操作
tasklist.delete;
break;//跳出这个任务循环
end;
end;


end;

end.
 
要类型转换的,还有你有两个tasklist哦
 
转什么类型呢?谢谢了
 
task:=TThreadNameInfo (tasklist.items );
 
先谢了
还有错,显示:
[error]unit2.pas[124]:invalid typecast
 
type
PThreadNameInfo = ^TThreadNameInfo ;
task:=PThreadNameInfo(tasklist.items );
 
//我给改了下
unit Unit2;
interface
uses
Classes,StdCtrls,Windows,SysUtils,wininet;
var
CS:TRTLCriticalSection;
//定义全局临界区
type
t1 = class(TThread)
private
TmpNum: integer;
tasklist:TList;//定义一个任务队列

procedure SetName;
procedure DataMemo;
protected
procedure Execute;
override;
public
constructor Create(Num: integer);
end;
var
tasklist: TList;//定义一个任务队列
type

TTaskRec = Record
ID :Integer;
// 任务唯一标示
RuningFlag:boolean;//正在执行标志
xxx:String;//需要执行的任务
end;

implementation
uses Unit1;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure t1.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{$IFDEF MSWINDOWS}
type
TThreadNameInfo = record
FType: LongWord;
// must be 0x1000
FName: PChar;
// pointer to name (in user address space)
FThreadID: LongWord;
// thread ID (-1 indicates caller thread)
FFlags: LongWord;
// reserved for future use, must be zero
end;
{$ENDIF}
{ t1 }
procedure t1.SetName;
{$IFDEF MSWINDOWS}
var
ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
ThreadNameInfo.FType := $1000;
ThreadNameInfo.FName := 't1';
ThreadNameInfo.FThreadID := $FFFFFFFF;
ThreadNameInfo.FFlags := 0;
try
RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
except
end;
{$ENDIF}
end;


constructor t1.Create(Num: integer);
begin
TmpNum := Num;
// 传递参数
FreeOnTerminate := True;
// 自动删除
InitializeCriticalSection(CS);
//初始化临界区
inherited Create(False);
// 直接运行
end;

procedure T1.DataMemo;
begin
while (form1.ListBox1.Items.Count > 0)do
begin
form1.ListBox2.Items.Add(form1.ListBox1.Items[0]+'--'+inttostr(TmpNum));
form1.ListBox1.Items.Delete(0);
form1.statusbar1.Panels[1].Text:=inttostr(form1.listbox1.Items.Count)+'--'+inttostr(form1.listbox2.Items.Count);

end;
end;

procedure t1.Execute;
var
task: ^TTaskRec;
i: integer;
begin
SetName;
while not Terminateddo
begin
if tasklist.Count<=0 then
begin
//没有任务时等待
Sleep(10);
Continue;
end;
For i:=0 to tasklist.Count-1do
begin
task:=tasklist.items;
// task:=TThreadNameInfo (tasklist.items );

if task.RuningFlag then
begin
//已经在执行,则返回继续判断下一个
Sleep(10);
continue;
end;
task.RuningFlag:=true;
EnterCriticalSection(cs);
//进入临界区
Synchronize(DataMemo);
// 同步
LeaveCriticalSection(CS);
//执行完成后从队列中删除,要注意线程保护这个任务的删除,添加操作
tasklist.delete(i);
break;//跳出这个任务循环
end;
end;


end;

end.
 
好不成~谢谢了,能留个QQ号码吗?
 
改了不止一处错误,编译没问题了,不知道能否正常运行。[8D]
 
后退
顶部