線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1157
推到 Plurk!
推到 Facebook!

寫Thread程式出現的錯誤

尚未結案
a126154362
一般會員


發表:21
回覆:13
積分:6
註冊:2004-05-30

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-10-25 23:32:37 IP:61.70.xxx.xxx 未訂閱
各位大大好  小弟正在學用Thread寫聊天室    會出現"List index out of bounds(-1) "的錯誤訊息 有大大知道這是什麼問題阿...........感謝    以下是我的Srever端部分程式:(綠色部分是Thread程式) ############################(Unit1)################################## //-------------------------------------------------------------------- #include  #include "jpeg.hpp" #pragma hdrstop #include "Unit1.h" #include "Unit2.h" #include "Unit3.h" //-------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; TThread *Char1; TThread *Char2; AnsiString CharNo,Username; AnsiString User1[8]={""}; AnsiString User2[8]={""}; int i=0,x=0,y=0; //--------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Char1=new Char1Thread(true); Char2=new Char2Thread(true); } //-------------------------------------------------------------------- void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender, TCustomWinSocket *Socket) { TStrings* RecMsg; AnsiString test,id,pw,login; RecMsg = new TStringList(); RecMsg->Text = Socket->ReceiveText(); id = RecMsg->Values["ID"]; pw = RecMsg->Values["PW"]; login = RecMsg->Values["TYPE"]; delete RecMsg; if(login==0) { ADOQuery1->Close(); ADOQuery1->SQL->Clear(); ADOQuery1->Parameters->CreateParameter("checkID",ftString,pdInput,30,id); ADOQuery1->Parameters->CreateParameter("checkPassword",ftString,pdInput,30,pw); ADOQuery1->SQL->Add("Select * From 資料表1 Where ID=:checkID and Password=:checkPassword"); ADOQuery1->Open(); if(ADOQuery1->RecordCount==1) { test="登入成功"; Label1->Caption=test; ServerSocket1->Socket->Connections[i-1]->SendText(test); } else { test="登入失敗"; Label1->Caption=test; ServerSocket1->Socket->Connections[i-1]->SendText(test); } } else if(login == 1) { ADOQuery1->Close(); ADOQuery1->SQL->Clear(); ADOQuery1->Parameters->CreateParameter("checkID",ftString,pdInput,30,id); ADOQuery1->Parameters->CreateParameter("checkPassword",ftString,pdInput,30,pw); ADOQuery1->SQL->Add("Select * From 資料表1 Where ID=:checkID"); ADOQuery1->Open(); if(ADOQuery1->RecordCount==1) { test="此帳號有人使用"; Label1->Caption=test; ServerSocket1->Socket->Connections[i-1]->SendText(test); } else { ADOQuery1->Close(); ADOQuery1->SQL->Clear(); ADOQuery1->Parameters->CreateParameter("checkID",ftString,pdInput,30,id); ADOQuery1->Parameters->CreateParameter("checkPassword",ftString,pdInput,30,pw); ADOQuery1->SQL->Add("Insert Into 資料表1 Values(:checkID,:checkPassword)"); ADOQuery1->ExecSQL(); test="此帳號註冊成功"; Label1->Caption=test; ServerSocket1->Socket->Connections[i-1]->SendText(test); } } else { test="無登入和註冊(遊客)"; Label1->Caption=test; ServerSocket1->Socket->Connections[i-1]->SendText(test); } } //-------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { ServerSocket1->Active=true; ServerSocket2->Active=true; ServerSocket3->Active=true; ServerSocket4->Active=true; ServerSocket5->Active=true; Char1->Resume(); Char2->Resume(); Button1->Enabled=false; Button2->Enabled=true; } //-------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { ServerSocket1->Active=false; ServerSocket2->Active=false; ServerSocket3->Active=false; ServerSocket4->Active=false; ServerSocket5->Active=false; Char1->Suspend(); Char2->Suspend(); Button1->Enabled=true; Button2->Enabled=false; } //-------------------------------------------------------------------- void __fastcall TForm1::ServerSocket2ClientRead(TObject *Sender, TCustomWinSocket *Socket) { AnsiString mag,port1; TStrings* RecMsg; RecMsg = new TStringList(); RecMsg->Text = Socket->ReceiveText(); Username=RecMsg->Values["Name"]; CharNo=RecMsg->Values["CharNo"]; delete RecMsg; TMemoryStream *msFile=new TMemoryStream; MYPACK mp; int j,k; if(CharNo==1) { port1=8003; mag="Port1=" port1 "\r\n"; Socket->SendText(mag); msFile->LoadFromFile("Char1.jpg"); for(k=0;k<8;k ) { if(User1[k]=="") { User1[k]=Username; break; } } } else { port1=8004; mag="Port1=" port1 "\r\n"; Socket->SendText(mag); msFile->LoadFromFile("Char2.jpg"); for(k=0;k<8;k ) { if(User2[k]=="") { User2[k]=Username; break; } } } int block=(msFile->Size%MAX_DATASIZE==0)? msFile->Size/MAX_DATASIZE: msFile->Size/MAX_DATASIZE 1; for(j=0; jSize; mp.dwThisDataSize=((j 1)*MAX_DATASIZE>msFile->Size)? msFile->Size-j*MAX_DATASIZE: MAX_DATASIZE; msFile->Position=j*MAX_DATASIZE; msFile->Read(mp.pbThisData, mp.dwThisDataSize); ServerSocket3->Socket->Connections[i-1]->SendBuf(&mp, sizeof(MYPACK)); Sleep(10); } delete msFile; } //-------------------------------------------------------------------- void __fastcall TForm1::ServerSocket1ClientConnect(TObject *Sender, TCustomWinSocket *Socket) { i=i 1; } //-------------------------------------------------------------------- void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject *Sender, TCustomWinSocket *Socket) { i=i-1; } //-------------------------------------------------------------------- void __fastcall TForm1::ServerSocket4ClientConnect(TObject *Sender, TCustomWinSocket *Socket) { x=x 1; } //-------------------------------------------------------------------- void __fastcall TForm1::ServerSocket4ClientDisconnect(TObject *Sender, TCustomWinSocket *Socket) { x=x-1; } //-------------------------------------------------------------------- void __fastcall TForm1::ServerSocket5ClientConnect(TObject *Sender, TCustomWinSocket *Socket) { y=y 1; } //-------------------------------------------------------------------- void __fastcall TForm1::ServerSocket5ClientDisconnect(TObject *Sender, TCustomWinSocket *Socket) { y=y-1; } //-------------------------------------------------------------------- #####################(Unit2)Thread##################### //-------------------------------------------------------------------- #include #pragma hdrstop #include "Unit2.h" #include "Unit1.h" #pragma package(smart_init) //-------------------------------------------------------------------- // Important: Methods and properties of objects in VCL can only be // used in a method called using Synchronize, for example: // // Synchronize(UpdateCaption); // // where UpdateCaption could look like: // // void __fastcall Char1Thread::UpdateCaption() // { // Form1->Caption = "Updated in a thread"; // } //-------------------------------------------------------------------- __fastcall Char1Thread::Char1Thread(bool CreateSuspended) : TThread(CreateSuspended) { } //-------------------------------------------------------------------- void Char1Thread::SetName() { THREADNAME_INFO info; info.dwType = 0x1000; info.szName = "Char1"; info.dwThreadID = -1; info.dwFlags = 0; __try { RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD),(DWORD*)&info ); } __except (EXCEPTION_CONTINUE_EXECUTION) { } } //-------------------------------------------------------------------- void __fastcall Char1Thread::Execute() { SetName(); //---- Place thread code here ---- AnsiString Username,Message,Nouser,msg; TStrings* RecMsg; RecMsg = new TStringList(); RecMsg->Text = Form1->ServerSocket4->Socket->ReceiveText(); Username=RecMsg->Values["User"]; Message=RecMsg->Values["Message"]; delete RecMsg; int k,j; for(k=0;k<8;k ) { if(Form1->User1[k]==Username) { Nouser=IntToStr(k); break; } } msg="NoUser=" Nouser "\r\nMessage=" Message "\r\n"; for(j=0;j<Form1->x;j ) { Form1->ServerSocket4->Socket->Connections[j]->SendText(msg); } }
Ktop_Robot
站務副站長


發表:0
回覆:3511
積分:0
註冊:2007-04-17

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-04-18 17:33:37 IP:000.000.xxx.xxx 未訂閱
提問者您好:


以上回應是否已得到滿意的答覆?


若已得到滿意的答覆,請在一週內結案,否則請在一週內回覆還有什麼未盡事宜,不然,
將由版主(尚無版主之區域將由副站長或站長)自由心證,選擇較合適之解答予以結案處理,
被選上之答題者同樣會有加分獎勵同時發問者將受到扣 1 分的處分。不便之處,請見諒。


有問有答有結案,才能有良性的互動,良好的討論環境需要大家共同維護,感謝您的配合。

------
我是機器人,我不接受簡訊.
系統時間:2024-11-27 20:35:12
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!