i want to terminate some specified threads? all i want to do is this: i am creating threads for clients connect to servers by createthread function; i call this function in my onclickconnect() button call, for each selected server in the listbox, i create a thread and do the operations; startconnection, receive message from server etc. I pass the connected ip-ports in some other list and I want to have a disconnect button. when i select ip-ports in the "successful connections" list and click on the button, i want the corresponding threads to terminate. I have done some coding and this works fine for several steps. When I try to connect the server, it connects. When I try to disconnect from the server the thread is terminated. HOWEVER when I try to connect to the disconnected server it gives an error and my program terminates. I dont want such things. If you can examine my code and give me some advice, I will be grateful. Thanks
Here is my connect/disconnect button:
here is my clientthread function:Code:if(m_con1dis2 == 1) //connect { m_fails.ResetContent(); m_success.ResetContent(); Selected = 0; //number of the servers selected from the list for(int i=0; i< m_ctlServerList.GetCount();i++) { if(m_ctlServerList.GetCheck(i) == 1 ) { Selected++; } } //no line is selected: if(Selected == 0) AfxMessageBox(L"No Server is Selected!"); CString *diziPtr= new CString[Selected]; CString *ptr= new CString[Selected]; char str[2]; CString svar; for(int i=0; i< m_ctlServerList.GetCount();i++) { if(m_ctlServerList.GetCheck(i) == 1 ) { _itoa_s(i,str,10); svar = str; *diziPtr = lines[i]+_T(":")+svar+_T("-")+m_savepath; ++diziPtr; } } for(int i=0;i<Selected;i++) { --diziPtr; Client_Thread[Selected-i-1]=CreateThread(NULL,0,ClientThread,(void *)diziPtr,0,&Client_ThreadID); } while(done==true) { } for(int i=0; i< m_ctlServerList.GetCount();i++) { if(m_ctlServerList.GetCheck(i) == 1 ) { _itoa_s(i,str,10); svar = str; *ptr = lines[i]+_T(":")+svar; if(connected[i] == 2) { m_success.AddString(*ptr); } else if(connected[i] == 1) { m_fails.AddString(*ptr); } ++ptr; UpdateData(false); } } thread = 0; done = true; }//end of connect button if(m_con1dis2 == 2) //disconnect { CString strTxt,thnum; int tnum; toDisconnect = 0; for(int i=0; i< m_success.GetCount(); i++) { if(m_success.GetSel(i) == 1) { toDisconnect++; m_success.GetText(i,strTxt); MessageBox(strTxt); AfxExtractSubString(thnum, strTxt, 2, ':');//third substr after : is the threadno tnum = _wtoi(thnum); //CString to int TerminateThread(Client_Thread[tnum],0); CloseHandle(Client_Thread[tnum]); AfxMessageBox(L"You terminated thread!"); //system("PAUSE"); //exit(8); } } for(int i=0; i< m_success.GetCount(); i++) //disconnect { if(m_success.GetSel(i) == 1) { m_success.DeleteString(i); m_fails.AddString(strTxt); } } UpdateData(false); if(m_success.GetCount() == 0) { m_ctlConnect.SetWindowTextW(_T("Connect")); m_con1dis2 = 1; } }//end of disconnect button }
Code:DWORD WINAPI ClientThread(void * num) { /*signal( SIGINT, &signal_handler ); signal( SIGTERM,&signal_handler ); signal( SIGABRT,&signal_handler ); */ CString ipport= *(CString * ) num; CString ipAddress,savepath; CString ports,socknums; int port,socknum; AfxExtractSubString(ipAddress, ipport, 0, ':'); //first substr upto : is the ip AfxExtractSubString(ports, ipport, 1, ':');//second substr after : is the port port = _wtoi(ports);//CString to int AfxExtractSubString(socknums, ipport, 2, ':');//third substr after : is the threadno AfxExtractSubString(savepath, ipport, 1, '-');//second substr after - is the savepath socknum = _wtoi(socknums); //CString to int CStringA ip(ipAddress); //CString to const char * Socket sockClient(socknum); if(sockClient.ConnectToServer(ip, port,socknum)== 0) //connection failed { connected[socknum] = 1; thread++ ; } else { connected[socknum] = 2; thread++ ; sockClient.timefile(ipAddress,ports,savepath);//creating the ip_port.txt file and write the time in it sockClient.RecvData(ipAddress,ports,socknum,savepath); } if(thread == Selected) //Selected and thread are global int { done = false; } return 0; }



LinkBack URL
About LinkBacks


