|
what if 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:
if(m_con1dis2 == 1)
{
m_fails.ResetContent();
m_success.ResetContent();
Selected = 0;
for(int i=0; i< m_ctlServerList.GetCount();i++)
{
if(m_ctlServerList.GetCheck(i) == 1 )
{
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;
}
if(m_con1dis2 == 2)
{
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, ':');
tnum = _wtoi(thnum);
TerminateThread(Client_Thread[tnum],0);
CloseHandle(Client_Thread[tnum]);
AfxMessageBox(L"You terminated thread!");
}
}
for(int i=0; i< m_success.GetCount(); i++)
{
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;
}
}
}
here is my clientthread function:
DWORD WINAPI ClientThread(void * num)
{
CString ipport= *(CString * ) num;
CString ipAddress,savepath;
CString ports,socknums;
int port,socknum;
AfxExtractSubString(ipAddress, ipport, 0, ':');
AfxExtractSubString(ports, ipport, 1, ':');
port = _wtoi(ports);
AfxExtractSubString(socknums, ipport, 2, ':');
AfxExtractSubString(savepath, ipport, 1, '-');
socknum = _wtoi(socknums);
CStringA ip(ipAddress);
Socket sockClient(socknum);
if(sockClient.ConnectToServer(ip, port,socknum)== 0)
{ connected[socknum] = 1;
thread++ ;
}
else
{
connected[socknum] = 2;
thread++ ;
sockClient.timefile(ipAddress,ports,savepath);
sockClient.RecvData(ipAddress,ports,socknum,savepath);
}
if(thread == Selected)
{
done = false;
}
return 0;
}
the index of the Client_Thread is the threadno given in the creation.
|
|
|
|
|
when I try to connect to the disconnected server it gives an error and my program terminates.
Please debug in this scenario to find out the point where the program crashes and post it here.
TerminateThread doesn't give the thread an opportunity to clean up the resources. So use some other mechanisms to terminate thread.
|
|
|
|
|
hi,
i am facing C4309 warning while compiling my program. can anyone help me how to solve this warning?
--------------
code snippet:
CString ws;
if (ws.ReverseFind( '>>' ) > -1 )
----------------------------
Thanks,
Rakesh.
|
|
|
|
|
Rakesh5 wrote: i am facing C4309 warning
Please, next time post the full error message, it would avoid people to have to google to know what it is exactly (we don't know all the error codes by heart you know).
Rakesh5 wrote: if (ws.ReverseFind( '>>' ) > -1 )
You have to use ">>" instead of '>>' . Simple quotes (') are used for characters and double quotes (") are used for strings. Here your string will be truncated to a single character.
|
|
|
|
|
Sorry for that inconvenience Mr.Moonen. Hereafter, i will try to give detailed picture while posting.
When i tried with ">>" , it was throwing an error message.
error C2664: 'ReverseFind' : cannot convert parameter 1 from 'char [3]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast.
can u please tell me where am i coding wrong?
Thanks,
Rakesh.
|
|
|
|
|
ReverseFind only works with single characters. it does not search for strings.
ReverseFind[^]
|
|
|
|
|
ya correct. what else can we do to avoid this warning then sir?
|
|
|
|
|
Search for a single character or use CString::Find[^] multiple times until the string can't be found anymore (and use the last returned index).
|
|
|
|
|
int p = str.ReverseFind('>');
if (p > 0)
if (str.GetAt(p-1)=='>')
then you found a match
|
|
|
|
|
IDE : vc 6.0
I want reduce my project build time, project is quite big one.
Is there any way?
|
|
|
|
|
You can use precompiled headers (however this option is enabled by default). After that, move all the includes to files that doesn't change frequently (e.g. windows.h , math.h and so on) into the header file used to build the precompiled header (usually stdafx.h ).
|
|
|
|
|
ganesh.dp wrote: project is quite big one.
What is big ?
Check for superfluous #include directives, check for badly coupled files (again, #include) dependencies.
If all looks ok, then check to see if you need to rebuild everything everytime. (are there some libraries/dll than can be build once in while ? instead of everytime ?)
If all else fails, have a look at distributed build system (xoreax[^] comes to mind, and it still support VC6).
M.
Watched code never compiles.
|
|
|
|
|
If the code resides on network drive, its take a huge time
|
|
|
|
|
There's loads of ways to speed up build time by writing your source code to build fast:
- The main one is reduce dependencies between compilation units. Don't include things that you can get away with forward declaring and don't do daft things like have single include files with global lists of error codes
- Use interfaces and parameterise from above (PFA) to decouple class implementations from each other
- Use PIMPLs to hide implementation details of classes. Don't use protected or private member functions in your base classes
- Use DLLs and libraries to help you reduce dependencies and DLLs to reduce link time
- (Controversial) Don't use precompiled headers, or if you must only include stuff that's not going to change (standard library headers, third party library headers e.g. boost headers, windows headers). Hiding OS interfaces under portable ones will help speed up your builds as using portable interfaces don't require dragging in loads of OS headers
Generally if you're talking less than a second on average to compile each source file and less than 10 seconds for a link there's probably not a lot of reason to try and speed things up more. Another option would be to invest in a newer compiler - VC++2010 has loads of advantages for building over VC6 even if you don't care about the other things like standards compliance and C++0x features.
Have a look in "Exceptional C++" by Herb Sutter and "Large Scale C++ Design" by John Lakos for more ideas along these lines.
Cheers,
Ash
|
|
|
|
|
Hi all,
I am trying to write to a port using CreateFile() and WriteFile() API's, but my WriteFile() is returning zero and on calling GetLastError() its returning error code 997 ERROR_IO_PENDING. I Know how to read a file when this happens but i am not getting how to write the file. Can anybody help me in doing this...
Here is the code
serial_handle = CreateFile(port_arg, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
success = WriteFile(serial_handle ,&temp,5,&temp1,&overlappRead);
I am not getting how to move ahead in this case
Thanks in advance
|
|
|
|
|
WriteFile
OVERLAPPED Structure
See: If the function fails, or is completing asynchronously, the return value is zero (FALSE)
NoteThe GetLastError code ERROR_IO_PENDING is not a failure; it designates the write operation is pending completion asynchronously.
So your Write operation may be pending, so try checking 'Internal' member of OVERLAPPED structure or do Wait on its 'hEvent' member.
|
|
|
|
|
If serial_handle was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile may return before the write operation has been completed. In this case, WriteFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the write operation.
If serial_handle was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the write operation starts at the current file position and WriteFile does not return until the operation has been completed.
The former is asynchoronous and the latter is synchoronous.
about your question,you can invoke "GetOverlappedResult(
HANDLE hFile, // handle to file, pipe, or device
LPOVERLAPPED lpOverlapped, // overlapped structure
LPDWORD lpNumberOfBytesTransferred, // bytes transferred
BOOL bWait // wait option
);" this function to get result of WriteFile();
eg:
success = WriteFil(serial_handle ,&temp,5,&temp1,&overlappRead);
if(!success)
{
DWORD dwErrorCode = GetLastError();
switch(dwErrorCode)
{
case ERROR_IO_PENDING://IO is operating
{
GetOverlappedResult(serial_handle,&overlapped,&temp1,TRUE);
//infinitely wait for WriteFile() operation compelete
break;
}
//others error code
default:
{
break
}
}
}
|
|
|
|
|
Hi all ... I'm a 3rd year computer engineering student ... And i do have an idea but so far we learn how to make client-server using " sockets " but the message queues is new method for me ... I'm required to do this project about how to develop authentication server that serves multiple clients at a time
the client gives the name and password as request and the server authenticates the request and returns the result ... ALL communications between client and server should use message queues techniques ...and i want it in c language ... Can u help me with the code ... appreciate it soo much ..
|
|
|
|
|
do not cross post. You already have asked this on the ATL forum
|
|
|
|
|
Hi Ralph, you have already posted a question about this problem in the Q&A section. Please, don't post the same question to multiple forums.
To get a good answer the best is to choose the forum that better match your requirements and post your question only there.
|
|
|
|
|
ok .. sorry guys I'm new user here .. i'm trying to get used to it ana learn ... any help with my question ??11
|
|
|
|
|
ralph 2 wrote: I'm new user here
Well you should have read this[^] first.
It's time for a new signature.
|
|
|
|
|
I did .. thanx ... I noticed that you r guys just Criticize not actually giving help .. if i knew that would be your attitude i wouldn't ask for help at all .. thanx anyway
|
|
|
|
|
ralph 2 wrote: I noticed that you r guys just Criticize not actually giving help
Maybe because you didn't ask any clear question. Where are you stuck exactly ? What did you already try ? What is the problem ?
Nobody is going to write your application for you.
|
|
|
|
|
I know how to use the system calls an prepare the msg queues then send or receive it but the problem with me is how to Authenticate a user in a previously stored database let say " txt file " ... I tried to make two threads the 1st writ a message and the second receive it ..
|
|
|
|
|