I am using Rob’s serial port method in Dashboard3D and everything is working well (I’m about half done) but when I go to close the port, the CloseHandle function never returns! The code is as follows:
HANDLE hSerial; //Opened in the constructor with CreateFile, etc.
CSerial::~CSerial(){CloseHandle(hSerial);}
I have debuged it up to that point so it does get to the CloseHandle function but it never passes. It took me two days to even figure out why the process wouldn’t end in WinXP! Any suggestions? (I don’t want to “just leave it open”…)
I’ve never heard of CloseHandle() not returning… is hSerial a valid handle? It hasn’t already been closed? Are you 100% sure?
Other than that (an invalid handle), the only thing I can think of is that the file is still in use (being written to by another thread?) so the handle can’t be invalidated.
It is definately a valid handle (I checked for an address in debug) but the app does use multithreading to read from the port. I wasn’t sure how to close the thread.
void SerialThread(PVOID pvoid);
_beginthread(SerialThread,0,NULL);<–In the CSerial constructor
I figured I would use _endthread() but then I realized that there is no way to tell it what thread to end. Even though there is only one thread running, I would like to be able to end a specific one and not all in general…
An update on the problem:
It appears that it isn’t just the CloseHandle() that won’t return. I tried to do _endthread() before it and that wouldn’t return either. I think it has to do with my class deconstructor. These calls are in there and they are not working… Any ideas?
PS: Also, if anyone knows why this upper line (the code) seems to be splitting about half-way through the page width, please explain. The rest of this message spans fine…
You have to call _endthread from within the thread you are trying to end. Also, nothing will execute after an _endthread() as it functions very similarly to a “return”.
If you want to avoid multi-threading, download RoboCon from my website and look through its source. It uses the built-in timeouts to accomplish a very similar thing to multithreading.
I’ll try using a bool to check if running and, if not, I’ll use _endthread(). I actually don’t mind multithreading. It was just starting with it that took some getting used to. I got the program to get input today but it seems to be giving me strange numbers… Example: the p2wheel variable goes from -2 to -127 (up to mid-way) and then from 127->2 (mid-way to down)… I have no clue…
unwraps a hermetically sealed project from ages gone by
I believe the function you’re looking for is TerminateThread(). It’s a bit inelegant, but it works quite well It’s first argument is the thread handle, and the other I’m not quite sure of… passing zero accomplishes my goal, but I can’t remember what it does.