添加文件到列表中行并加入到队列Tlist中,然后用动态拷贝文件任务,调试执行拷贝了任务,而实际上并没有拷贝文件到目的路径下,各位帮帮!!
程序如下:
//**主程序***//
unit Unit1;interfaceuses
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;
implementationuses 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.Add do
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[I] := TTaskThread.Create(G_TaskList); { //添加任务到 G_TaskList中
G_TaskList:=TList.Create;
Task1:=TTask.Create;
for i:=0 to ListView1.Items.Count-1 do
begin
if ListView1.Items.Item[i].Checked then
begin
New(ARecord);
ARecord^.Name:=ListView1.Items.Item[i].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-1 do
begin
ARecord:= G_TaskList.Items[i];
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[I] := 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 Unit1;interfaceuses
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;
implementationuses 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.Add do
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[I] := TTaskThread.Create(G_TaskList); { //添加任务到 G_TaskList中
G_TaskList:=TList.Create;
Task1:=TTask.Create;
for i:=0 to ListView1.Items.Count-1 do
begin
if ListView1.Items.Item[i].Checked then
begin
New(ARecord);
ARecord^.Name:=ListView1.Items.Item[i].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-1 do
begin
ARecord:= G_TaskList.Items[i];
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[I] := 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.
解决方案 »
- ListBox透明问题
- 请问如何用delphi代码实际复制一个正在被别的程序打开的forpro的dbf数据库文件。
- 用Animate控件来显示avi文件的问题
- 读取UNIX文本文件的问题
- RAVE的问题
- [求助]获得系统中各文件的当前日期的Delphi语句怎样写?
- 怎样使TCHART变成3D和2D?还有怎样在个种图形之间切换?
- 用delphi做实时流问题好不好?
- 我要做个项目,为毕业设计所作的,大家来发言啊!!
- 请问各位Delphi的常用函数到哪里可以找到?
- 怎么给idTcpClient加上代理(proxy)??
- 想计算一个时间差,可SQL得出的结果是1900-01-01 02:00:00.000,如何能够换算成小时?
unit uTTaskThread;interfaceuses
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;implementationuses 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-1 do //遍历 theMlist队列中的所有任务
begin
new(ARecord);
ARecord:= theMlist.Items[i]; //取出任务
Task1:=ARecord.Data;
Form1.ListView1.Items[i].SubItems.Text:=Task1.TaskStaus; //显示任务状态
if Task1.StausFlag=1 then //若任务处于等待,执行任务
Task1.CopyFileToDir; //任务类执行文件拷贝
Dispose(ARecord);
sleep(50);
end;
end;end.
Task1.CopyFileToDir; //任务类执行文件拷贝
这个是把任务类的strFileName:string;//源文件
DecName:String; //目的路径
拷贝 的