Мне нужно загрузить в поток содержимое memo так сказать.
так как программа будет многопоточная нужно написать в критической секции код, чтобы поток не хватал одну и ту же строку из memo (я так предпологаю). не знаю что писать в критической секции. помогите.
дальше...
гет запрос на URl адрес и как дополнительный параметр при запросе одна из строк из memo. дальше вторая и т.д.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IdHttp,SyncObjs;
type
TForm2 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Thr = class(TThread)
private
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean);
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
var
Thread, Acc:integer;
CrS:TcriticalSection;
Work:boolean;
SList:Tstringlist;
constructor Thr.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
end;
procedure Thr.Execute;
var
url,result:string;
CAcc:integer;
HTTP:TIdHTTP;
id:string;
begin
while Work do
begin
CrS.Enter;
Inc(Acc);
if Acc<SList.Count then CAcc:=Acc else Work:=false;
CrS.Leave;
if Work then
begin
id:=SList[CAcc];
url:='http://site.com/?id='+id;
HTTP:=TIdHTTP.create;
result:=Http.get(url);
HTTP.Free;
end;
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
i:integer;
begin
SList.Clear;
for I := 0 to Memo1.lines.Count - 1 do
SList.Add(Memo1.Lines[i]);
Acc:=-1;
Work:=true;
for Thread:=1 to strtoint(Edit1.Text) do
Thr.Create(false);
Thread:=strtoint(Edit1.Text);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
SList:=Tstringlist.create;
CrS:=TcriticalSection.create;
end;
end.
[Ссылки могут видеть только зарегистрированные пользователи. ]
Последний раз редактировалось uma1805; 18.11.2011 в 12:22.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IdHttp,SyncObjs;
type
TForm2 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Thr = class(TThread)
private
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean);
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
var
Thread, Acc:integer;
CrS:TcriticalSection;
Work:boolean;
SList:Tstringlist;
constructor Thr.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
end;
procedure Thr.Execute;
var
url,result:string;
CAcc:integer;
HTTP:TIdHTTP;
id:string;
begin
while Work do
begin
CrS.Enter;
Inc(Acc);
if Acc<SList.Count then CAcc:=Acc else Work:=false;
CrS.Leave;
if Work then
begin
id:=SList[CAcc];
url:='http://site.com/?id='+id;
HTTP:=TIdHTTP.create;
result:=Http.get(url);
HTTP.Free;
end;
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
i:integer;
begin
SList.Clear;
for I := 0 to Memo1.lines.Count - 1 do
SList.Add(Memo1.Lines[i]);
Acc:=-1;
Work:=true;
for Thread:=1 to strtoint(Edit1.Text) do
Thr.Create(false);
Thread:=strtoint(Edit1.Text);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
SList:=Tstringlist.create;
CrS:=TcriticalSection.create;
end;
end.
[Ссылки могут видеть только зарегистрированные пользователи. ]
при 1 потоке все нормально, как только ставлю 2 или более, выскакивает ошибка.
помогите ))
код:
Код:
procedure TNewThread.Execute;
var
s1:string;
CAcc:integer;
HTTP:TIdHTTP;
id:string;
begin
while Work do
begin
CrS.Enter;
Inc(Acc);
if Acc<SList.Count then CAcc:=Acc else Work:=false;
CrS.Leave;
if Work then
begin
id:=SList[CAcc];
s1:=Form1.http.Get('http://сайт'+id);
if Pos('"back"',s1)<>0 then
begin
Rez:=1;
end
else
Rez:=-1;
synchronize(Sync);
end;
end;
end;
p.s. сама ошибка:
ProjecProject.exe raised exception class EAccessViolation with message 'Acess violation at adress 0040457C in module 'Project.exe'. Write of adress 00000018'. Process Stopped. Use Step or Run to continue.
Последний раз редактировалось *Хalyava*; 18.11.2011 в 18:34.
при 1 потоке все нормально, как только ставлю 2 или более, выскакивает ошибка.
помогите ))
код:
Код:
procedure TNewThread.Execute;
var
s1:string;
CAcc:integer;
HTTP:TIdHTTP;
id:string;
begin
while Work do
begin
CrS.Enter;
Inc(Acc);
if Acc<SList.Count then CAcc:=Acc else Work:=false;
CrS.Leave;
if Work then
begin
id:=SList[CAcc];
s1:=Form1.http.Get('http://сайт'+id);
if Pos('"back"',s1)<>0 then
begin
Rez:=1;
end
else
Rez:=-1;
synchronize(Sync);
end;
end;
end;
p.s. сама ошибка:
ProjecProject.exe raised exception class EAccessViolation with message 'Acess violation at adress 0040457C in module 'Project.exe'. Write of adress 00000018'. Process Stopped. Use Step or Run to continue.
Могу с нуля помочь кодом, а то твой хрен разберёшь.
1. Объяви глобальную переменную, например:
Код:
var
Account_Now : Integer;
2. В потоке объяви локальную переменную, например: