|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionUsing 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 about. DetailsFirst, 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 #pragma once
__gc class CMyThreads
{
public:
CMyThreads(void);
~CMyThreads(void);
void MyThreadProc();
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
arguments. (See Calling the thread from another class: foo()
{
CMyThreads * pMyThread;
pMyThread = new CMyThreads;
pMyThread->AddArguments(Argument1, Argument2);
ThreadStart * pThread = new ThreadStart(pMyThread, &CMyThreads::MyThreadProc);
Thread *oThread = new Thread(pThread);
oThread->Start();
}
Before we create #include "StdAfx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
#include <stdio.h>
#include "mythreads.h"
CMyThreads::CMyThreads(void)
{
}
CMyThreads::~CMyThreads(void)
{
}
void CMyThreads::MyThreadProc()
{
Console::WriteLine(S"Starting Thread... ");
Thread::Sleep(5);
pClass->ExternalFunction(/*Arguments*/);
Console::WriteLine(S"Finishing Thread...");
}
void CMyThreads::AddArguments(void* pArg1, void* pArg2)
{
m_FirstArgument = pArg1;
m_SecondArgument = pArg2;
}
Conclusion
Remember to
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||