跪求Idhttp问题的解决方案 ( 积分: 200 )

  • 主题发起人 主题发起人 qingtz
  • 开始时间 开始时间
Q

qingtz

Unregistered / Unconfirmed
GUEST, unregistred user!
我在用idhttp来post网页的时候窗体就死了,后来用了IdAntiFreeze后在post一个网页的时候还能解决,可是用for循环post一组网页的时候就会又死了,谁知道怎么解决?
下面是代码
单个网页post的时候我调用ref(id,id);两个参数的值相等,就相当于循环一次,这时候窗口不会死
多个网页post的时候我调用ref(id1,id2);这个时候就死了,要等到全部post完才能缓过来
求各位大侠帮忙解决一下^_^ 如何把下面的代码改成多线程在后台运行,希望附上代码,能用的话我给加分

procedure Ref(beginid,endid:integer);
var
m,i,a,b,c,j:integer;
username,password,billadd,email,key,dealstr,resultstr,d:string;
RSL:tstringlist;
begin
RSL:=TStringList.Create;
statsset(beginid,endid); //调用函数 将第2个id的状态全改为等待中(因为不会编多线程,只能一个一个的等
for m:=beginid to endid do
begin
d:='';
if stopctrl=true then //stopctrl是全局变量,当post多个网页的时候形同虚设,窗口一死更本无法改变其值
break;
with form1.ClientDataSet1 do
begin
IndexFieldNames:='ID';
if FindKey([m]) then
begin
username:=fieldvalues['用户名'];
password:=fieldvalues['密码'];
billadd:=fieldvalues['帐户地址'];
email:=fieldvalues['注册邮箱'];
key:= fieldvalues['关键字'];
end;
end;
rsl.Add('username='+username);
rsl.Add('password='+password);
rsl.Add('email='+email);
rsl.Add('user='+username);
rsl.Add('pw='+password);
rsl.Add('action=Login to My Accoun');
form1.ClientDataSet1.Edit;
form1.ClientDataSet1.FieldValues['状态']:='连接中...';
form1.ClientDataSet1.post;
try
form1.IdAntiFreeze1.OnlyWhenIdle:=false;
dealstr:=stringreplace(ansilowercase(form1.IdHTTP1.post(billadd,RSL)),' ','',[rfreplaceall]);//post网页并给dealstr赋值
b:=length(dealstr); //从这儿开始在dealstr中搜索需要的字符
if pos(key,dealstr)>0 then
for c:=pos(key,dealstr) to b-10 do
if dealstr[c]='$' then
begin
i:=c+1;
break;
end;
for j:=i to i+10 do
if (dealstr[j]='.') or ((dealstr[j]>='0') and (dealstr[j]<='9')) then
begin
d:=d+dealstr[j]; //搜索完成并把找到的字符赋值给d
end;
with form1.ClientDataSet1 do //把d写入ClientDataSet1,并更新状态
begin
IndexFieldNames:='ID';
if FindKey([m]) then
form1.ClientDataSet1.Edit;
form1.ClientDataSet1.FieldValues['当前金额']:=strtofloat(d);
form1.ClientDataSet1.FieldValues['状态']:='更新完成';
form1.ClientDataSet1.FieldValues['更新日期']:=now;
form1.ClientDataSet1.post;
end;
rsl.Clear;
except
form1.ClientDataSet1.Edit;
form1.ClientDataSet1.FieldValues['状态']:='更新失败';
form1.ClientDataSet1.FieldValues['更新日期']:=now;
form1.ClientDataSet1.post;
rsl.Clear;
end;
application.ProcessMessages;
end;
rsl.Destroy;
end;

procedure statsset(beginid,endid:integer);
var
m:integer;
begin
for m:=beginid+1 to endid do
with form1.ClientDataSet1 do
begin
IndexFieldNames:='ID';
if FindKey([m]) then
begin
form1.ClientDataSet1.Edit;
fieldvalues['状态']:='等待中...';
form1.ClientDataSet1.post;
end;
end;
end;

跪求多线程代码
 
我在用idhttp来post网页的时候窗体就死了,后来用了IdAntiFreeze后在post一个网页的时候还能解决,可是用for循环post一组网页的时候就会又死了,谁知道怎么解决?
下面是代码
单个网页post的时候我调用ref(id,id);两个参数的值相等,就相当于循环一次,这时候窗口不会死
多个网页post的时候我调用ref(id1,id2);这个时候就死了,要等到全部post完才能缓过来
求各位大侠帮忙解决一下^_^ 如何把下面的代码改成多线程在后台运行,希望附上代码,能用的话我给加分

procedure Ref(beginid,endid:integer);
var
m,i,a,b,c,j:integer;
username,password,billadd,email,key,dealstr,resultstr,d:string;
RSL:tstringlist;
begin
RSL:=TStringList.Create;
statsset(beginid,endid); //调用函数 将第2个id的状态全改为等待中(因为不会编多线程,只能一个一个的等
for m:=beginid to endid do
begin
d:='';
if stopctrl=true then //stopctrl是全局变量,当post多个网页的时候形同虚设,窗口一死更本无法改变其值
break;
with form1.ClientDataSet1 do
begin
IndexFieldNames:='ID';
if FindKey([m]) then
begin
username:=fieldvalues['用户名'];
password:=fieldvalues['密码'];
billadd:=fieldvalues['帐户地址'];
email:=fieldvalues['注册邮箱'];
key:= fieldvalues['关键字'];
end;
end;
rsl.Add('username='+username);
rsl.Add('password='+password);
rsl.Add('email='+email);
rsl.Add('user='+username);
rsl.Add('pw='+password);
rsl.Add('action=Login to My Accoun');
form1.ClientDataSet1.Edit;
form1.ClientDataSet1.FieldValues['状态']:='连接中...';
form1.ClientDataSet1.post;
try
form1.IdAntiFreeze1.OnlyWhenIdle:=false;
dealstr:=stringreplace(ansilowercase(form1.IdHTTP1.post(billadd,RSL)),' ','',[rfreplaceall]);//post网页并给dealstr赋值
b:=length(dealstr); //从这儿开始在dealstr中搜索需要的字符
if pos(key,dealstr)>0 then
for c:=pos(key,dealstr) to b-10 do
if dealstr[c]='$' then
begin
i:=c+1;
break;
end;
for j:=i to i+10 do
if (dealstr[j]='.') or ((dealstr[j]>='0') and (dealstr[j]<='9')) then
begin
d:=d+dealstr[j]; //搜索完成并把找到的字符赋值给d
end;
with form1.ClientDataSet1 do //把d写入ClientDataSet1,并更新状态
begin
IndexFieldNames:='ID';
if FindKey([m]) then
form1.ClientDataSet1.Edit;
form1.ClientDataSet1.FieldValues['当前金额']:=strtofloat(d);
form1.ClientDataSet1.FieldValues['状态']:='更新完成';
form1.ClientDataSet1.FieldValues['更新日期']:=now;
form1.ClientDataSet1.post;
end;
rsl.Clear;
except
form1.ClientDataSet1.Edit;
form1.ClientDataSet1.FieldValues['状态']:='更新失败';
form1.ClientDataSet1.FieldValues['更新日期']:=now;
form1.ClientDataSet1.post;
rsl.Clear;
end;
application.ProcessMessages;
end;
rsl.Destroy;
end;

procedure statsset(beginid,endid:integer);
var
m:integer;
begin
for m:=beginid+1 to endid do
with form1.ClientDataSet1 do
begin
IndexFieldNames:='ID';
if FindKey([m]) then
begin
form1.ClientDataSet1.Edit;
fieldvalues['状态']:='等待中...';
form1.ClientDataSet1.post;
end;
end;
end;

跪求多线程代码
 
不需要使用多线程,你用个线程就行了。你自己去找一些例子吧。
 
看着乱,我写过一个比较简单的多线程的例子,比较简单的演示了,在线程实际技术,但是界面上有一个进度效果,供参考
窗体文件
object Form1: TForm1
Left = 192
Top = 107
Width = 516
Height = 251
BorderIcons = [biSystemMenu, biMinimize]
Caption = '演示线程发送消息'
Color = clBtnFace
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 96
TextHeight = 12
object Button1: TButton
Left = 196
Top = 92
Width = 75
Height = 25
Caption = '暂停/回复'
TabOrder = 0
OnClick = Button1Click
end
object ProgressBar1: TProgressBar
Left = 0
Top = 208
Width = 508
Height = 16
Align = alBottom
Min = 0
Max = 100
TabOrder = 1
end
end

主窗口单元文件
unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, MyThread;

type
TForm1 = class(TForm)
Button1: TButton;
ProgressBar1: TProgressBar;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Thread: TMyThread;
public
{ Public declarations }
procedure Info(var msg: TMessage); message WM_Info;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
Thread := TMyThread.Create(True, Self.Handle, 100000);
end;

destructor TForm1.Destroy;
begin
Thread.Terminate;
inherited;
end;

procedure TForm1.Info(var msg: TMessage);
var
P: TInfo;
begin
if msg.msg = WM_Info then begin
P := TInfo(Pointer(msg.LParam)^);
ProgressBar1.Max := P.nTotal;
ProgressBar1.Min := 0;
ProgressBar1.Position := P.nPos;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if Thread.Suspended then
Thread.Resume
else
Thread.Suspend;
end;

end.

线程单元文件
unit MyThread;

interface

uses
Classes, Messages, windows;

const
WM_Info = WM_USER + 100;

type
TInfo = record
nPos, nTotal: Integer;
end;

TMyThread = class(TThread)
private
{ Private declarations }
FMainHandle : THandle;
nPos : Integer;
nTotal : Integer;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean; _MainHandle : THandle; _Total: Integer);reintroduce;
end;

implementation


{ TMyThread }

constructor TMyThread.Create(CreateSuspended: Boolean;
_MainHandle: THandle; _Total: Integer);
begin
Inherited Create(CreateSuspended);

FMainHandle := _MainHandle;
nPos := 0;
nTotal := _Total;
end;

procedure TMyThread.Execute;
var
p : TInfo;
begin
while not Terminated do begin
if nPos < nTotal then begin
p.nPos := nPos;
p.nTotal := nTotal;
Inc(nPos);
if FMainHandle <> 0 then
SendMessage(FMainHandle, WM_Info, 0, Integer(@P));
end;
end;
end;

end.
添加到你的工程就可以了,如果不行留下email,我发工程给你
 
to: chenybin
谢谢你能提供代码,先给你50分,
我看了你的代码,可是与我提供的代码似乎没有一点关系,没有idhttp,clientdataset!对我的软件没有什么用!
麻烦您先帮我分析一下我的代码,看能不能改成多线程(我希望创建5个线程按照beginid-->endid的顺序更新clientdataset中的记录)?能不能解决我上面出现的窗口死的情况
有什么和我代码相似工程麻烦发到我的邮箱email: fordel@eyou.com
 
对不起,刚才忘了给分
 
idhttp提交多个网页窗口死的问题已经自己解决了!上面的代码没有错,只是自己犯了个低级错误!^_^
剩下的问题是怎么把我的代码用多线程实现(5个线程)?
 
邮件已发,不过被退回了,又发了一次
 
to:chenybin
我昨天把工程文件发给你了,麻烦你帮改一下,我不急,什么时候改好就发给我,谢谢
 

Similar threads

后退
顶部