请教高手多线程与数据库的问题!(100分)

  • 主题发起人 主题发起人 pengxuan7591
  • 开始时间 开始时间
P

pengxuan7591

Unregistered / Unconfirmed
GUEST, unregistred user!
自己写了一个多线程上传的例子,但是在上传过程中偶尔会出现问题:10个文件只上传了7个或8个,或有时候就能全部上传.
unit U_FTPClient;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdFTP, ExtCtrls, Grids, DBGrids, DB, ADODB, StdCtrls, IdIntercept,
IdLogBase, IdLogEvent;

type
TMyFirstThread = class(TThread)
private
id:string;
filename:string;
protected
procedure Execute;override;
end;

type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
Memo1: TMemo;
IdLogEvent1: TIdLogEvent;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
i:integer=1;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var Thread1:TMyFirstThread;
begin
ADOQuery1.First;
while not ADOQuery1.Eof do
begin
Thread1:=TMyFirstThread.Create(true);
Thread1.id:=ADOQuery1.FieldByName('F_ID').AsString;
Thread1.filename:=ADOQuery1.FieldByName('F_WJ').AsString;
Thread1.Resume;
ADOQuery1.Next;
end;
end;

{ TMyFirstThread }

procedure TMyFirstThread.Execute;
var IdFTP:TIdFTP;
begin
IdFTP:=TIdFTP.Create(nil);
try
try
IdFTP.Host:='10.113.58.253';
IdFTP.Port:=21;
IdFTP.Username:='aa';
IdFTP.Password:='123';
IdFTP.Connect;
IdFTP.List(nil);
Form1.Memo1.Lines.Add(IntToStr(Form1.Memo1.Lines.Count+1)+' '+ExtractFileName(Self.filename)+' '+'准备上传!');
IdFTP.Put(Self.filename,ExtractFileName(Self.filename));
Form1.Memo1.Lines.Add(IntToStr(Form1.Memo1.Lines.Count+1)+' '+ExtractFileName(Self.filename)+' '+'上传成功!');
except
Form1.Memo1.Lines.Add(Self.id+ExtractFileName(Self.filename)+'上传失败!');
end;
finally
IdFTP.Free;
end;
end;

end.
哪位朋友帮忙看看/谢谢
 
帮你提前
 
对INDYFTP用多线程互斥 理论上没问题了
 
procedure TMyFirstThread.Execute;
var IdFTP:TIdFTP;
begin
inherited; //加这个
(你下面的代码)
。。。
 
后来我改成这样
procedure TForm1.Button1Click(Sender: TObject);
var Thread1:TMyFirstThread;
begin
ADOQuery1.First;
while not ADOQuery1.Eof do
begin
Thread1:=TMyFirstThread.Create(true);
Thread1.id:=ADOQuery1.FieldByName('F_ID').AsString;
Thread1.filename:=ADOQuery1.FieldByName('F_WJ').AsString;
Thread1.Resume;
ADOQuery1.Next;
Sleep(0);//加了一句这个,循环测试了几十次,好像目前为止没有什么问题了.但时间久了还会出错.
end;
end;
但是我用的Indy的FTPServer Demo却又有问题了,
出的错的:
Exception EAccessViolation in module FTPServer_console.exe at 00000000.Access violation at address 0042030B in module 'FTPServer_console.exe'.Read of address 000000C.
哪位朋友能给看看这是什么错误或者给一个稳定的FTPServer程序.
 
indy本身应该不会有问题,你用了indy9吧,还有,楼主的线程最好为ado组件加入同步代码,因为ado是异步执行的,不安全,还有楼主的线程释放没看到代码,不懂是没释放还是楼主没贴出来

奇怪了,声明在私有变量也能访问吗? 线程可以这样,我以前怎没试过,嘻嘻
 
解决问题了,是因为IndyServer服务器的问题,IndyServer对多线程处理的不好.
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部