回 帖 发 新 帖 刷新版面

主题:请教多线程,下面程序为什么会出错?

unit Unit1; 

interface 

uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, 
  IdHTTP; 

type 
  TForm1 = class(TForm) 
    Button1: TButton; 
    IdHTTP1: TIdHTTP; 
    procedure Button1Click(Sender: TObject); 
  private 
    procedure up; 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 

var 
  Form1: TForm1; 

implementation 

{$R *.dfm} 
procedure TForm1.up; 
var s:string; 
begin 
  try 
    s:=idhttp1.Get( 'http://www.zhcw.com/data-js/nowdata98.js ');出错的地方 
  except 
    showmessage( '更新失败! '); 
  end; 
  if s < > ' ' then showmessage( '更新成功! '); 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
var tid:dword; 
begin 
  CreateThread(nil,0,@tform1.up,nil,0,Tid); 
end; 

end. 

First chance exception at $7C812A5B. Exception class EAccessViolation with message  'Access violation at address 00482985 in module  'up.exe '. Read of address 00000364 '. Process up.exe (2320)

回复列表 (共1个回复)

沙发

你出错的问题很大,
首先一个进程中包括很多线程,其中最主要的是主线程,各个线程之间是独立的,有各自的线程变量。,@tform1.up属于主线程 然而辅助线程想访问另一个线程的东西 必然出错。在就是 其他辅助线程中不要出现 显示之类的信息
s 全局变量
procedure up
begin
  try
      s:=form1.idhttp1.Get(s);//出错的地方
   form1.Memo1.Text:=s;
  except
  //  showmessage( '更新失败! ');
  end;
 // if s < > ' ' then showmessage( '更新成功! ');
end;

我来回复

您尚未登录,请登录后再回复。点此登录或注册