threadstart.cpp
来自「Visual_C++[1].NET_Bible1 Visual_C++宝典书中」· C++ 代码 · 共 65 行
CPP
65 行
#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
using namespace System;
using namespace System::Threading;
__gc class CWorkerThread
{
public:
CWorkerThread();
public:
void Start();
protected:
void ThreadMethod();
protected:
Thread* m_pThread;
};
CWorkerThread::CWorkerThread()
{
m_pThread = new Thread(new ThreadStart(this, &CWorkerThread::ThreadMethod));
}
void CWorkerThread::ThreadMethod()
{
Console::WriteLine("[ThreadMethod] Thread initialized");
for (int i = 0; i < 5; i++)
{
Console::WriteLine("[ThreadMethod] Thread Performing task {0}",
__box(i+1));
Thread::Sleep(500);
}
Console::WriteLine("[ThreadMethod] Thread finished working");
}
void CWorkerThread::Start()
{
m_pThread->Start();
}
int _tmain(void)
{
CWorkerThread* pThread;
for (int i = 0; i < 2; i++)
{
Console::WriteLine("[_tmain] Starting thread {0}", __box(i));
pThread = new CWorkerThread();
pThread->Start();
}
Console::WriteLine("[_tmain] Finished creating threads");
Console::ReadLine();
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?