📄 threadsleep.cpp
字号:
#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
#include <math.h>
using namespace System;
using namespace System::Threading;
__gc class CWorkerThread
{
public:
CWorkerThread(int iThreadId, int nTasksToPerform);
public:
void Start();
void Suspend();
void Resume();
void Abort();
protected:
void ThreadMethod();
protected:
Thread* m_pThread;
protected:
int m_iThreadId;
protected:
int m_nTasksToPerform;
public:
__property int get_TotalTasks()
{
return m_nTasksToPerform;
}
protected:
int m_iCurrentTask;
public:
__property int get_CurrentTask()
{
return m_iCurrentTask;
}
protected:
bool m_bWorkCompleted;
};
CWorkerThread::CWorkerThread(int iThreadId, int nTasksToPerform)
: m_iThreadId(iThreadId),
m_nTasksToPerform(nTasksToPerform),
m_iCurrentTask(0),
m_bWorkCompleted(false)
{
// Create thread
m_pThread = new Thread(new ThreadStart(this, &CWorkerThread::ThreadMethod));
}
void CWorkerThread::Start()
{
// Start thread
if (m_pThread)
{
m_pThread->Start();
}
}
void CWorkerThread::Suspend()
{
if (m_pThread)
{
m_pThread->Suspend();
}
}
void CWorkerThread::Resume()
{
if (m_pThread)
{
m_pThread->Resume();
}
}
void CWorkerThread::Abort()
{
if (m_pThread && !m_bWorkCompleted)
{
m_pThread->Abort();
}
}
void CWorkerThread::ThreadMethod()
{
for (int i = 0; i < m_nTasksToPerform; i++)
{
m_iCurrentTask++;
Thread::Sleep(5000);
}
m_bWorkCompleted = true;
}
__gc class CMyApplication
{
public:
CMyApplication() : m_pThread(NULL), m_bThreadPaused(false) {}
protected:
CWorkerThread* m_pThread;
public:
void Run();
public:
String* ProcessMenu();
bool ProcessUserOption(String* pstrResponse);
protected:
bool m_bThreadPaused;
};
void CMyApplication::Run()
{
m_pThread = new CWorkerThread(1, 10);
m_pThread->Start();
bool bUserQuit = false;
do
{
String* pstrResponse = ProcessMenu();
bUserQuit = ProcessUserOption(pstrResponse);
} while (!bUserQuit);
m_pThread->Abort(); // CWorkerThread will check to see if still running
}
String* CMyApplication::ProcessMenu()
{
String* pstrResponse;
Console::WriteLine("*** MENU ***");
Console::WriteLine("1. View thread's status");
Console::WriteLine("2. Pause thread's statues");
Console::WriteLine("3. Resume thread");
Console::WriteLine("4. Quit");
Console::Write("==>");
pstrResponse = Console::ReadLine();
Console::WriteLine("\n");
return pstrResponse;
}
bool CMyApplication::ProcessUserOption(String* pstrResponse)
{
bool bUserQuit = false;
if (0 == pstrResponse->CompareTo("1"))
{
Console::WriteLine("Thread status : {0} tasks of {1} tasks completed",
__box(m_pThread->CurrentTask),
__box(m_pThread->TotalTasks));
Console::Write("Thread is currently ");
if (m_bThreadPaused)
Console::WriteLine("SUSPENDED.");
else
Console::WriteLine("EXECUTING.");
Console::WriteLine();
}
else if (0 == pstrResponse->CompareTo("2"))
{
this->m_pThread->Suspend();
this->m_bThreadPaused = true;
Console::WriteLine("Thread SUSPENDED!\n");
}
else if (0 == pstrResponse->CompareTo("3"))
{
this->m_pThread->Resume();
this->m_bThreadPaused = false;
Console::WriteLine("Thread RESUMED!\n");
}
else if (0 == pstrResponse->CompareTo("4"))
bUserQuit = true;
return bUserQuit;
}
int _tmain(void)
{
CMyApplication* pApp = new CMyApplication();
pApp->Run();
Console::Write("Application finished. Press <Enter> to close window.");
Console::ReadLine();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -