套用THread 無法 Run |
缺席
|
jacike
一般會員 發表:17 回覆:22 積分:7 註冊:2003-11-17 發送簡訊給我 |
請問大大幫我看看為什麼在我套用THread後就不能正常copyfile,在非THread下就正常?2.在thread unit內的 setname 是否需要修改?3.小弟的寫法是不是可行,還是有地方需要改的(書上寫的都含含糊糊搞得我快日夜顛到了),如果邏輯上有問題麻煩大大幫小弟看一下要如何修改,感恩 <textarea class="delphi" rows="10" cols="60" name="code">unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Buttons;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
pgb: TProgressBar;
Label1: TLabel;
Edit1: TEdit;
Edit2: TEdit;
procedure BitBtn1Click(Sender: TObject);
procedure CopyFileWithProgressBar1(Const sourcefilename, targetfilename: String );
private
//var source,det : string;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses cp;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
//source:=edit1.Text;
//det:=edit2.Text;
copypro.Create(false);
//CopyFileWithProgressBar1('z:\test.exe','d:\test.exe');
end;
procedure TForm1.CopyFileWithProgressBar1(Const sourcefilename, targetfilename: String );
//var
//fs,t: TStream;
//buff: array [0..65535] of Byte; // buffer
//i : integer;
begin
{fs := TFileStream.Create(sourcefilename, fmOpenRead);
T := TFileStream.Create( targetfilename,fmOpenWrite or fmCreate );
pgb.Max:=fs.Size;
try
while fs.Position < fs.Size do
begin
fs.Read(buff, SizeOf(buff));
t.Write(buff,sizeof(buff));
pgb.Position := t.Position;
for I := 0 to 65535 do
begin
label1.Caption:=inttostr(pgb.Position i);
end;
end;
finally
fs.Free;
end;
}
end;
end.
//=============================
unit cp;
interface
uses
Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF}, unit1, SysUtils;
type
copypro = class(TThread)
private
procedure SetName;
protected
procedure Execute; override;
end;
implementation
{ 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 copypro.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}
{ copypro }
procedure copypro.SetName;
{$IFDEF MSWINDOWS}
var
ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
ThreadNameInfo.FType := $1000;
ThreadNameInfo.FName := 'copypro';
ThreadNameInfo.FThreadID := $FFFFFFFF;
ThreadNameInfo.FFlags := 0;
try
RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
except
end;
{$ENDIF}
end;
procedure copypro.Execute;
var
fs,t: TStream;
buff: array [0..65535] of Byte; // buffer
i : integer;
begin
fs := TFileStream.Create(form1.Edit1.Text, fmOpenRead);
T := TFileStream.Create( form1.Edit2.Text,fmOpenWrite or fmCreate );
form1.pgb.Max:=fs.Size;
try
while fs.Position < fs.Size do
begin
fs.Read(buff, SizeOf(buff));
t.Write(buff,sizeof(buff));
form1.pgb.Position := t.Position;
for I := 0 to 65535 do
begin
form1.label1.Caption:=inttostr(form1.pgb.Position i);
end;
end;
finally
fs.Free;
end;
SetName;
{ Place thread code here }
end;
end.
</textarea>
|
syntax
尊榮會員 發表:26 回覆:1139 積分:1258 註冊:2002-04-23 發送簡訊給我 |
copypro.Resume;
或 copypro.Execute; ===================引 用 jacike 文 章=================== 請問大大幫我看看為什麼在我套用THread後就不能正常copyfile,在非THread下就正常?2.在thread unit內的 setname 是否需要修改?3.小弟的寫法是不是可行,還是有地方需要改的(書上寫的都含含糊糊搞得我快日夜顛到了),如果邏輯上有問題麻煩大大幫小弟看一下要如何修改,感恩 <textarea name="code" cols="60" rows="10" class="delphi">unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, Buttons; type TForm1 = class(TForm) BitBtn1: TBitBtn; pgb: TProgressBar; Label1: TLabel; Edit1: TEdit; Edit2: TEdit; procedure BitBtn1Click(Sender: TObject); procedure CopyFileWithProgressBar1(Const sourcefilename, targetfilename: String ); private //var source,det : string; { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses cp; {$R *.dfm} procedure TForm1.BitBtn1Click(Sender: TObject); begin //source:=edit1.Text; //det:=edit2.Text; copypro.Create(false); //CopyFileWithProgressBar1('z:\test.exe','d:\test.exe'); end; procedure TForm1.CopyFileWithProgressBar1(Const sourcefilename, targetfilename: String ); //var //fs,t: TStream; //buff: array [0..65535] of Byte; // buffer //i : integer; begin {fs := TFileStream.Create(sourcefilename, fmOpenRead); T := TFileStream.Create( targetfilename,fmOpenWrite or fmCreate ); pgb.Max:=fs.Size; try while fs.Position < fs.Size do begin fs.Read(buff, SizeOf(buff)); t.Write(buff,sizeof(buff)); pgb.Position := t.Position; for I := 0 to 65535 do begin label1.Caption:=inttostr(pgb.Position i); end; end; finally fs.Free; end; } end; end. //============================= unit cp; interface uses Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF}, unit1, SysUtils; type copypro = class(TThread) private procedure SetName; protected procedure Execute; override; end; implementation { 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 copypro.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} { copypro } procedure copypro.SetName; {$IFDEF MSWINDOWS} var ThreadNameInfo: TThreadNameInfo; {$ENDIF} begin {$IFDEF MSWINDOWS} ThreadNameInfo.FType := $1000; ThreadNameInfo.FName := 'copypro'; ThreadNameInfo.FThreadID := $FFFFFFFF; ThreadNameInfo.FFlags := 0; try RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo ); except end; {$ENDIF} end; procedure copypro.Execute; var fs,t: TStream; buff: array [0..65535] of Byte; // buffer i : integer; begin fs := TFileStream.Create(form1.Edit1.Text, fmOpenRead); T := TFileStream.Create( form1.Edit2.Text,fmOpenWrite or fmCreate ); form1.pgb.Max:=fs.Size; try while fs.Position < fs.Size do begin fs.Read(buff, SizeOf(buff)); t.Write(buff,sizeof(buff)); form1.pgb.Position := t.Position; for I := 0 to 65535 do begin form1.label1.Caption:=inttostr(form1.pgb.Position i); end; end; finally fs.Free; end; SetName; { Place thread code here } end; end. </textarea> |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |