⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadstart.cpp

📁 Visual_C++[1].NET_Bible1 Visual_C++宝典书中的全部源码
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -