The problem is that the program hangs after I call the StopThread function. I am sure I need to signal the function that my thread has terminated but, since I am new to the MFC and multithreaded programming, I am not sure where to do that.
Never call AfxEndThread, or any similar function. It's bound create problems.
What happens here is that the compiler optimizes away the m_bRunning in your thread since the value is not changed inside the while statement.
Fix this by declaring the m_bRunning variable as 'volatile'.
I assume you start your thread with a call to AfxBeginThread.
If so, you should create it suspended and set the CWinThread::m_bAutoDelete to false to be able to wait for the thread to finish. Delete the CWinThread pointer that you got from AfxBeginThread in your StopThread function.
If you don't do this, a race condition will occur that may result in the thread handle you are waiting for is invalid.
Hope this helps
It's supposed to be hard, otherwise anybody could do it!
all threads must be terminated before the main thread exits or the program will still be resident in memory
This isn't true - In fact if the "main" thread exits the process exits and any other threads that may be executing are killed on the spot. This is why when exiting the main thread typically waits for any worker threads to finish (using WaitForsingleObject or friends) before bailing: this gives the wokers a chance to clean up after themselves (for example, delete temp files).
PS: As for how to properly terminate a thread, the correct answer is ask the thread to exit and then wait for it to do so.
Where can I get a complete list of Win32 error codes. In VC++5.0, am getting a Win32 error 3, the authors say its because of the path specified not found, searched the whole net, but still can't make out what exactly is causing this error...
True, the System Error codes provide a description, but its the same generic description. Could you tell me what might be causing this runtime error? am specifying all the paths of the DLL,exe,LIB. Am i missing any other environment settings...any help on that?
But are they the correct paths? Are they absolute or relative?
AM giving absolute paths of the DLL and the .exe of my project in the Tools-options-directories and also filling up the System PATH vairable with the same paths, though am still getting that error.
do you have an idea, what are the general locations the loader looks for while executing an .exe ?
I usually try to set the error code to display errors when this happens. SetErrorMode(0) so that all errors are displayed. When all errors are displayed, Windows will tell you which DLL is missing or exported function is not found that your application is trying to load or find. Usually, an application has the error mode set to something other than 0 because it is 'handling' the errors.
Try that and see if you get more information from your program.
People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks
Look for winerror.h (in VC6, open a new text document, type winerror.h, select it, right-click on it and select "Open document") for most error codes you will ever encounter. Path not found is exactly that - like trying to open C:\BadDir\MoreBadDir\File.txt when the BadDir directory does not exist.
-=- JamesIf you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
Lets say that I want to see wich way of output is faster: "printf" or "cout". I would writte something like this:
int main( void )
time_t begin, end;
begin = time( NULL );
unsigned long i;
for ( i=0 ; i<4294967295 ; i++ )
printf( "%d\n", i ); // This is for printf. If I was testing cout it would be
// cout << i << endl;
end = time( NULL );
printf( "IT TOOK: %d\n", end - begin );
My intention is not to test wich of those is faster but to have an algorithm to test the speed for any application. The problem with this implementation is that it is not very precise; computers work a lot faster than what can be measured with seconds. Any suggestions?