Using Managed C++ to create threads is not as easy as C#. If you already
tried to compile a Managed C++ program with threads, you know what I am talking
First, do not try to add the thread in the same class, it won't
compile. The easiest way I personally found is to create a new managed
class ( remember to add
__gc in the beginning of the class declaration ).
In these instance we'll call it
CMyThreads. Why not? Every program should
have a CMySomething as a class.
__gc class CMyThreads
void AddArguments(void* pArg1, void* pArg2)
void * m_FirstArgument ;
void * m_SecondArgument ;
One problem in managed C++ threads is the arguments. You
must create a function to call before starting the thread if you want
Calling the thread from another class:
CMyThreads * pMyThread;
pMyThread = new CMyThreads;
ThreadStart * pThread = new ThreadStart(pMyThread, &CMyThreads::MyThreadProc);
Thread *oThread = new Thread(pThread);
Before we create
ThreadStart you must call
you want arguments on this thread.
The thread will not begin until you call the member function
using namespace System;
using namespace System::Threading;
Console::WriteLine(S"Starting Thread... ");
void CMyThreads::AddArguments(void* pArg1, void* pArg2)
m_FirstArgument = pArg1;
m_SecondArgument = pArg2;
Sleep to allow the main process to continue.
Also you put anything you like in
MyThreadProc() you can also call a
function in another class. I hope you have fun!