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

c++ builder多线程的一个有关问题

2012-03-24 
求助c++ builder多线程的一个问题我想在程序运行时,开一个线程执行以下代码:C/C++ codeAnsiString capTXM

求助c++ builder多线程的一个问题
我想在程序运行时,开一个线程执行以下代码:
   

C/C++ code
    AnsiString cap;    TXMLDocument *XMLDocument1;    XMLDocument1->LoadFromFile("http://xtayaitak.free.yun110.cn/mms.xml");    url= XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->Text;    cap= XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->AttributeNodes->Nodes[0]->Text;    label1->Caption=cap;


自己由于是第一次写多线程,所以没有成功。请教大家帮忙。

这是我自己写的程序
C/C++ code
//UnitThread.h#ifndef UnitThreadH#define UnitThreadH//---------------------------------------#include <Classes.hpp>#include <msxmldom.hpp>#include <XMLDoc.hpp>#include <xmldom.hpp>#include <XMLIntf.hpp>//---------------------------------------class TLabelSet : public TThread{private:    AnsiString url;    TLabel *label1;    void __fastcall downloadUrl();protected:    void __fastcall Execute();public:    __fastcall TLabelSet(bool CreateSuspended,AnsiString &url,TLabel *label1);};//---------------------------------------#endif


C/C++ code
//---------------------------------------/***************UnitThead.cpp***************/#include <vcl.h>#pragma hdrstop#include "UnitThread.h"#pragma package(smart_init)__fastcall TLabelSet::TLabelSet(bool CreateSuspended,AnsiString &url,TLabel *label1)    : TThread(CreateSuspended){    Priority=tpIdle;    FreeOnTerminate=true;}//---------------------------------------void __fastcall TLabelSet::Execute(){    //---- Place thread code here ----    Synchronize(downloadUrl);}//---------------------------------------void __fastcall TLabelSet:: downloadUrl(){    AnsiString cap;    TXMLDocument *XMLDocument1;    XMLDocument1->LoadFromFile("http://xtayaitak.free.yun110.cn/mms.xml");    url= XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->Text;    cap= XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->AttributeNodes->Nodes[0]->Text;    label1->Caption=cap;}


下面是运行多线程代码
C/C++ code
void __fastcall TFormMain::FormShow(TObject *Sender){    thread1 =new TLabelSet(false,url,Lable_4);}


[解决办法]
明白了,楼主的误区,认为传过来的参数名称和,成员名称一样,他就会自动赋值了
这样是不行的,必须在构造函数中给成员,赋值
C/C++ code
__fastcall TLabelSet::TLabelSet(bool CreateSuspended,AnsiString &Url,TLabel *Label1)    : TThread(CreateSuspended){    Priority=tpIdle;    FreeOnTerminate=true;    this->url=Url;    this->label1=Label1;} 

热点排行