首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

初学者搞多线程遇到有关问题,

2012-02-21 
菜鸟搞多线程遇到问题,求助啊...typeTThread1class(TThread)privateprotectedprocedureExecuteoverride

菜鸟搞多线程遇到问题,求助啊...


type
    TThread1   =   class(TThread)
    private

    protected
        procedure   Execute;   override;
    public
        constructor   create;
end;


var
    Form1:   TForm1;
    stri,surl,ak:string;
    startNum:integer;
    finishNum:integer;
    i:integer=0;

var
    Th1,Th2:TThread1;

implementation

{$R   *.dfm}


procedure   TForm1.FormCreate(Sender:   TObject);
begin
    IdHTTP1.HandleRedirects:=True;
    IdHTTP1.Request.UserAgent   :=   'Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   98;   Maxthon) ';
    IdHTTP1.Request.ContentType:= 'application/x-www-form-urlencoded ';
end;

constructor   Tthread1.create;
begin
inherited   Create(True);
FreeOnTerminate   :=   False;
end;

procedure   TThread1.Execute;
var
    t:TstringList;
begin
            stri:=inttostr(i);
            t:=TStringList.Create;
            try
                    t.Add( 'id= '+stri);
                    t.Add( 'body= '+ansitoUTF8(form1.Memo1.Text));
                    form1.IdHTTP1.Post(sUrl,t);
            finally
            t.Free;
            end;
form1.Edit_sID.Text:=inttostr(i+1);
i:=i+1;

if   i=finishNum+1   then
    begin
    Th1.Suspend;
    Th2.Suspend;
    end;
end;


procedure   TForm1.Button2Click(Sender:   TObject);
begin
i:=strtoint(Edit_sID.Text);
finishNum:=strtoint(Edit_fID.Text);
sUrl:= 'http://XXXX.com/leaveMSG.do ';
Th1.Resume;
Th2.Resume;
end;

procedure   TForm1.FormDestroy(Sender:   TObject);
begin
Th1.Terminate;  
Th1.Destroy;
Th2.Terminate;  
Th2.Destroy;
end;

procedure   TForm1.Button3Click(Sender:   TObject);
begin
Th1.Suspend;
Th1.Suspend;
end;
----------------------
自动向ID连续的用户留言,Edit_sID.Text是开始ID,Edit_fID.Text是结束ID.finishNum是strtoint(Edit_fID.Text)..
对照一个多线程的例程自己做,点Button2错误~~


[解决办法]
1. 没看到Th1和Th2创建实例的代码。

2. 在线程类的Execute的操作可视化对象(比如form1.IdHTTP1),需要使用线程同步的方法。

3. 在线程类的Execute的最后执行
Th1.Suspend;
Th2.Suspend;
有些看不懂。

热点排行