Easy Way to Use Threads in Visual C++ (VC++)
by leevonk in Circuits > Software
48739 Views, 8 Favorites, 0 Comments
Easy Way to Use Threads in Visual C++ (VC++)

a bare minimum of what you need to use threads in VC++. Threads allow your program to do multiple things at the same time (multithreading) instead of in a linear sequence. For instance, you could have your program doing an infinately looped complex task and at the same time be waiting for the user to enter a 'stop' key. Using multithreading, the user could press the 'stop' key at _any_ time to stop the program.
#including the Propper Stuff

even if you have already started an app as a non MFC console application, you can use threads in your app.
1) First, go into setting and set it so that it uses MFC as a static library
2) Then add #include below the #include "stdafx" that should already be in your program
3) meanwhile, if you have
#include "windows.h" or #include "mmsystem.h"
put these two _above_ the #include "stdafx.h"
4) Be careful, the order in which you declare the #includes matters a lot!! (if the way you have it gives errors during compile, reorder them til it works).
1) First, go into setting and set it so that it uses MFC as a static library
2) Then add #include
3) meanwhile, if you have
#include "windows.h" or #include "mmsystem.h"
put these two _above_ the #include "stdafx.h"
4) Be careful, the order in which you declare the #includes matters a lot!! (if the way you have it gives errors during compile, reorder them til it works).
Write the Necessary Code

Below is all the code you need to add threads to your project.
UINT LeesThread( LPVOID pParam )
is the thread, put your thread code in there (it's like a function)
AfxBeginThread(LeesThread, TempChar);
is the code that starts your thread, in this example it's at the start of the main() function.
TempChar
is just a character pointer that needs to be sent to the thread for some reason, the value of it doesn't matter.
Note: This has worked fine for me in many programs, everywhere I read about how to make threads has been pretty complicated, so although this will work, it's probably not technically correct. I don't care, it works:
//#############################################
//############---CODE BELOW---#################
//#############################################
#include
char* TempChar;
UINT LeesThread( LPVOID pParam )
{
//--put your thread code here
//--use a while(TRUE) loop if you want it to run continuously
return 0;
}
void main()
{
//--Start your thread (in this case, LeesThread)
AfxBeginThread(LeesThread, TempChar);
}
UINT LeesThread( LPVOID pParam )
is the thread, put your thread code in there (it's like a function)
AfxBeginThread(LeesThread, TempChar);
is the code that starts your thread, in this example it's at the start of the main() function.
TempChar
is just a character pointer that needs to be sent to the thread for some reason, the value of it doesn't matter.
Note: This has worked fine for me in many programs, everywhere I read about how to make threads has been pretty complicated, so although this will work, it's probably not technically correct. I don't care, it works:
//#############################################
//############---CODE BELOW---#################
//#############################################
#include
char* TempChar;
UINT LeesThread( LPVOID pParam )
{
//--put your thread code here
//--use a while(TRUE) loop if you want it to run continuously
return 0;
}
void main()
{
//--Start your thread (in this case, LeesThread)
AfxBeginThread(LeesThread, TempChar);
}