多线程中变量问题,高手请答!(200)

K

kmwap

Unregistered / Unconfirmed
GUEST, unregistred user!
我在线程类中定义了abc := String;procedure WebB_Login;线程中执行abc := '123';WebBroser1.OnDocumentComplete := WebB_Login;当进入WebB_Login的时候abc就变为另一个值,有什么办法可以让他不变,abc不能为全局变量,因为每个线程中附的值不一样
 
让你再次访问这个帖子,一定想:为什么没人回答我呢,我都给了200分了.甚至可能会想:这么多人看都没人会,一定是我自己的水平太高了.让我无情的破碎你的想法吧,原因很简单,你的提问水平太差.你可以做以下选择:1 提高自己的提问水平(虽然这在短时间内几乎看不到效果,提问水平与个人技术水平息息相关)2 尽可能的贴出完整代码
 
不会答就不要说费话,这几行关键代码已经很简单!
 
对于线程的私有数据,可以这样保存:1。定义为线程类的私有变量,这样每个线程类就是唯一的。2。用threadvar关键字定义的变量,也会成为每个线程的私有变量,而不管这个线程创建了几个实例,都会有独立的数据。这是由os实现的线程存储机制,但似乎效率不那么高。
 
看来楼主做了第三种选择...真是自大的可怕
 
我用的就是第一种方法,在整个线程中都不会变,只要一进入WebB_Login过程,变量就为空了第二种方式定义,也是一样
 
而且我发现到了WebBroser1.OnDocumentComplete := WebB_Login;线程就会onTerminateit结束了
 
最终原因应是WebBroser1.OnDocumentComplete := WebB_Login ,导致线程结束,所以WebB_Login过种中定义的线程变量也就访问不到了
 
在线程中这样做 WebBroser1.OnDocumentComplete := WebB_Login ? 目的是什么, 一般是这样处理的 function TxxxThread.HandleManagerCommand(alist: TStringlist): Integer;var cmd_type, cmd_str, cmd_str1: string;
begin
if assigned(MarketEvent) then
begin
MarketEvent (self,cmd_type,cmd_str,cmd_str1);
end;
end;
然后 在外部 if assigned(g_xxxThread) then
g_xxxThread.MarketEvent :=do
tttRefresh;
 
贴出N年前写的线程类,楼主好好学习下吧.type TSeekDiverCallBack = procedure(MobileInfoArr: TMobileInfoArr) of object;
TUpLoadCallBack = procedure(UpLoadSize, FtpResult: Integer;
Data: Pointer) of object;
TCallBackThread = class(TThread) private FProc: TThreadMethod;
FCheckStatus: Integer;
FUpLoadCount: Integer;
procedure WithForCallBack;
protected procedure Execute;
override;
public constructor Create(CreateSuspended: Boolean;
Proc: TThreadMethod;
Status: Integer);
end;
implementation{ TCallBackThread }constructor TCallBackThread.Create(CreateSuspended: Boolean;
Proc: TThreadMethod;
Status: Integer);
begin
FProc := Proc;
FCheckStatus := Status;
if not Terminated then
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TCallBackThread.Execute;
begin
inherited;
WithForCallBack;
Synchronize(FProc);
end;
procedure TCallBackThread.WithForCallBack;
begin
if FCheckStatus = DIVICE_STATUS_SEND_FILE then
FUpLoadCount := FCtrl.AlreadySendSize;
while FCtrl.CtrlStatus = FCheckStatusdo
begin
if (FCheckStatus = DIVICE_STATUS_SEND_FILE) and (FUpLoadCount <> FCtrl.AlreadySendSize) then
begin
FUpLoadCount := FCtrl.AlreadySendSize;
Synchronize(FProc);
end;
Sleep(50);
end;
end;
 
WebB:Twebbrowser;NewTb:= TTabSheet.Create(Main_Form.PageC);WebB:= TWebBrowser.Create(NewTb);WebB.Align := alClient;WebBTheaterMode := True;WebB.Silent := True;TWinControl(WebB).Parent := NewTb;WebB.Navigate('http://www.abc.com');WebB.OnDocumentComplete := WebB_Login;要等webb 下载网页结束后进入过程WebB_Login处理smlab拜托不要抄写无关的代码来给我,线程定义我天天在写
 
因为有几万行代码,所以只能举个例随便写几行
 
果然是自大惹的货,你想要的答案就在我刚才发的"无关的"代码里,自己慢慢找吧
 
我自已找到原因了因为在线程创建时设置了 FreeOnTerminate := True;现在把这句去掉,执行了WebB.OnDocumentComplete := WebB_Login;虽然检测到线程的OnTerminate事件表示线程已结束,但是进入 WebB_Login过程后原来在过程中定义的变量仍然没有释放,就OK了,谢谢大家!
 

Similar threads

S
回复
0
查看
949
SUNSTONE的Delphi笔记
S
S
回复
0
查看
770
SUNSTONE的Delphi笔记
S
顶部