多线程管理任务问题!急救啊!!!!(100分)

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

meriko1981

Unregistered / Unconfirmed
GUEST, unregistred user!
添加文件到列表中行并加入到队列Tlist中,然后用动态拷贝文件任务,调试执行拷贝了任务,而实际上并没有拷贝文件到目的路径下,各位帮帮!!
程序如下:
//**主程序***//
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls,shellapi,uTTaskThread;
type
TTaskNodeRecord=Record
Name:string;
Data:Pointer;
end;
PTaskNodeRecord=^TTaskNodeRecord;

TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
OpenDialog1: TOpenDialog;
Edit1: TEdit;
Button2: TButton;
StatusBar1: TStatusBar;
ComboBox1: TComboBox;
Label1: TLabel;
Label2: TLabel;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
G_TaskList:TList;
ARecord:PTaskNodeRecord;
public
{ Public declarations }
end;

var
Form1: TForm1;
Threads: array of TTaskThread;
// 定义线程数组
//Thread1: TTaskThread;
implementation
uses uTakClass;

{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
//每添加一个任务便把它追加到Tlist中
var
i:integer;
strFileName:string;
Task1:TTask;
begin
if OpenDialog1.Execute then
begin
with ListView1.Items.Adddo
begin
Caption:= OpenDialog1.FileName;
SubItems.Add('等待任务。。');
end;

Task1:=TTask.Create;
New(ARecord);
ARecord^.Name:=OpenDialog1.FileName;
//任务结构体的Name为文件名
Task1.OFilerName:=OpenDialog1.FileName;
//任务类中的拷贝的源文件名
Task1.DecDir:=trim(Edit1.Text);
//任务类中的拷贝的目的路径
Task1.StausFlag:=1;
//任务处于等待状态
ARecord^.Data:=Task1;
//任务赋给Tlist队列节点
G_TaskList.Add(ARecord) //把整个任务加到队列
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
strFileName:string;
Task1:TTask;
begin
if not DirectoryExists(Edit1.Text) then
CreateDir(Edit1.Text);
if not DirectoryExists(Edit1.Text) then
begin
ShowMessage('目的文件夹不存在,请检查!');
Exit;
end;
Edit1.ReadOnly:=True;

//动态创建线程
SetLength(Threads,strToint(ComboBox1.Text));
for I := 0 to High(Threads)do
Threads := TTaskThread.Create(G_TaskList);
{ //添加任务到 G_TaskList中
G_TaskList:=TList.Create;
Task1:=TTask.Create;
for i:=0 to ListView1.Items.Count-1do
begin
if ListView1.Items.Item.Checked then
begin
New(ARecord);
ARecord^.Name:=ListView1.Items.Item.Caption;
Task1.OFilerName:=OpenDialog1.FileName;
Task1.DecDir:=trim(Edit1.Text);
Task1.StausFlag:=1;
ARecord^.Data:=Task1;
G_TaskList.Add(ARecord);
end;
end;
//添加任务完成 G_TaskList是一任务队列

//处理任务
{ for i:=1 to strToint(ComboBox1.Text)do
begin
Thread1:=TTaskThread.Create(G_TaskList,i);
end;
}
{ for i:=0 to G_TaskList.Count-1do
begin
ARecord:= G_TaskList.Items;
strFileName:=ExtractFilename(ARecord^.NAme);
CopyFile(pchar(ARecord^.NAme),pchar(edit1.Text+strFileName),False);
Dispose(ARecord);
end;
}
end;

{ TTask }

procedure TForm1.FormCreate(Sender: TObject);
begin

G_TaskList:=TList.Create;
//创建全局队列
{for i:=1 to strToint(ComboBox1.Text)do
begin
Thread1:=TTaskThread.Create(G_TaskList,i);
end;
}
{ SetLength(Threads,strToint(ComboBox1.Text));
for I := 0 to High(Threads)do
Threads := TTaskThread.Create(G_TaskList,i);
}
end;

end.
//*****Form1*****//
object Form1: TForm1
Left = 192
Top = 107
Width = 597
Height = 430
Caption = #22810#32447#31243#25991#20214#25335#36125#31649#29702
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 160
Top = 16
Width = 49
Height = 13
Caption = #32447#31243#25968
end
object Label2: TLabel
Left = 24
Top = 305
Width = 65
Height = 13
Caption = #30446#30340#36335#24452
end
object ListView1: TListView
Left = 56
Top = 48
Width = 401
Height = 249
Columns = <
item
Caption = #25991#20214#21517
Width = 300
end
item
Caption = #29366#24577
Width = 90
end>
GridLines = True
MultiSelect = True
RowSelect = True
TabOrder = 0
ViewStyle = vsReport
end
object Button1: TButton
Left = 224
Top = 336
Width = 75
Height = 25
Caption = #25191#34892#20219#21153
TabOrder = 1
OnClick = Button1Click
end
object Edit1: TEdit
Left = 96
Top = 304
Width = 297
Height = 21
ImeName = #20013#25991'('#31616#20307') - '#26234#33021' ABC'
TabOrder = 2
Text = 'C:/'
end
object Button2: TButton
Left = 472
Top = 128
Width = 75
Height = 25
Caption = #28155#21152#25991#20214
TabOrder = 3
OnClick = Button2Click
end
object StatusBar1: TStatusBar
Left = 0
Top = 384
Width = 589
Height = 19
Panels = <
item
Width = 50
end>
end
object ComboBox1: TComboBox
Left = 232
Top = 8
Width = 49
Height = 21
ImeName = #20013#25991'('#31616#20307') - '#26234#33021' ABC'
ItemHeight = 13
TabOrder = 5
Text = '1'
Items.Strings = (
'1'
'2'
'3'
'4'
'5'
'6'
'7'
'8'
'9'
'10')
end
object OpenDialog1: TOpenDialog
Left = 488
Top = 64
end
end
//*********************任务类**************************************//
unit uTakClass;
interface
uses
Classes,Windows;
type
TTask=Class //任务类
private
strFileName:string;
DecName:String;
strStaus:string;
intSaus:integer;
public
//拷贝文件的方法
procedure CopyFileToDir;
//源文件名(含路径)
property OFilerName:String read strFileName write strFileName;
//目的路径
property DecDir:string read DecName write DecName;
//任务状态
property TaskStaus:string read strStaus write strStaus;
//任务状态标记 1 等待 2 运行 3 停止
property StausFlag:integer read intSaus write intSaus;
end;

implementation
{ TTask }
procedure TTask.CopyFileToDir;
begin
strStaus:=strFileName+' 正在执行任务';
intSaus:=2;
CopyFile(pchar(strFileName),pchar(DecName),False);
strStaus:=strFileName+' 执行任务完毕';
intSaus:=3;
end;

end.
//*****************************线程类*************************************************//
unit uTTaskThread;
interface
uses
Classes,Windows,Dialogs;
type
TTaskNodeRecord=Record //存放任务的结构体
Name:string;
Data:Pointer;
end;
PTaskNodeRecord=^TTaskNodeRecord;
TTaskThread = class(TThread)
private
{ Private declarations }
ARecord:PTaskNodeRecord;
theMlist:TList;
procedure TaskExcute;
protected
procedure Execute;
override;
public
constructor Create(MList:Tlist);
end;

implementation
uses uTakClass, Unit1;

constructor TTaskThread.Create(MList:Tlist);
begin
inherited Create(False);
theMlist:=MList;
end;

procedure TTaskThread.Execute;
var
i:integer;
begin
{ Place thread code here }
Synchronize(TaskExcute);
end;

{ TTask }
procedure TTaskThread.TaskExcute;
//执行任务
var
i:integer;
Task1:TTask;
begin
Task1:=TTask.Create;
for i:=0 to theMlist.Count-1do
//遍历 theMlist队列中的所有任务
begin
new(ARecord);
ARecord:= theMlist.Items;
//取出任务
Task1:=ARecord.Data;
Form1.ListView1.Items.SubItems.Text:=Task1.TaskStaus;
//显示任务状态
if Task1.StausFlag=1 then
//若任务处于等待,执行任务
Task1.CopyFileToDir;
//任务类执行文件拷贝
Dispose(ARecord);
sleep(50);
end;
end;

end.
 
帮你调试了一下 copyfile你用错了.
改成 CopyFile(pchar(strFileName),pchar(DecName+ExtractFileName(strFileName)),False);
另外, 希望这只是你用来学习用的代码,否则, 太糟糕了, 混乱的思路,糟糕的代码
 
谢谢你 shangshang
的确是很混乱 是一道面试题,我没有思路 不知道要怎么做
开发一个类似Windows消息队列(Tlist)的程序,队列中的任务节点的结构采用Trecord方式存储:
TTaskNodeRecord=Pack record
Name:string;
Data:Tpointer;
end;
全局任务队列为C_TaskList:Tlist;
1.用Delphi编写一个Windows Form 程序
2.定义一线程处理类,对队列中任务进行处理,处理过程暂时用Sleep(50)来代替
3.可以在启动服务时设置服务的线程数量(1-5个)
4.运行过程中动态显示每一线程的工作状态信息,包括:运行状态,当前处理的任务数
5.可以在任务运行过程中动态批量添加任务
 
的确是很混乱 是一道面试题...
------------------------------------
无语中...
 
我刚接触线程,初学者,那位仁兄指点下迷津啊
谢谢了~
 
接受答案了.
 
后退
顶部