关于MfC线程方面的问题,求大神指教

小松风行 2014-04-27 02:06:59
在MFC基于对话框下,我想使用一个start按钮来创建进程,然后stop按钮来使进程挂起,然后contiune按钮来恢复进程,我没学过MFC,由于要完成任务,时间紧迫,还有两天就要交任务,所以求大神帮帮忙?
希望大神说的明白点,因为没学过MFC,所以最好能贴上源码,不要说的太笼统,


比如这样


一般创建过程如下:
先定义一个工作函数,一般来说你的线程就是依照该函数的功能执行任务:
UINT MyThreadFunction( LPVOID pParam )
{
//函数体
return 0;
}
然后可以按以下方式创建线程:
CWinThread* MyThread=AfxBeginThread(MyThreadFunction , pParam , THREAD_PRIORITY_NORMAL , 0 , 0 , NULL);
4、线程的等待与唤醒
(1)让线程等待(暂时挂起):
MyThread->SuspendThread();
(2)唤醒暂停的线程:
MyThread->ResumeThread();
5、查看线程状态:
DWORD code;
GetExitCodeThread(MyThread->m_hThread , &code);
if(code==STILL_ACTIVE){//线程仍在执行}
else {//线程停止执行}
6、结束线程
TerminateThread(MyThread->m_hThread , 0);


这样我不知道这些东西该放到哪啊,所以还是不会,求大神帮忙!
...全文
168 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-04-28
  • 打赏
  • 举报
回复
PROCESS_INFORMATION The PROCESS_INFORMATION structure is filled in by the CreateProcess function with information about a newly created process and its primary thread. typedef struct _PROCESS_INFORMATION { // pi HANDLE hProcess; HANDLE hThread; DWORD dwProcessId; DWORD dwThreadId; } PROCESS_INFORMATION; Members hProcess Returns a handle to the newly created process. The handle is used to specify the process in all functions that perform operations on the process object. hThread Returns a handle to the primary thread of the newly created process. The handle is used to specify the thread in all functions that perform operations on the thread object. dwProcessId Returns a global process identifier that can be used to identify a process. The value is valid from the time the process is created until the time the process is terminated. dwThreadId Returns a global thread identifiers that can be used to identify a thread. The value is valid from the time the thread is created until the time the thread is terminated. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winbase.h. See Also Processes and Threads Overview, Process and Thread Structures, CreateProcess
赵4老师 2014-04-28
  • 打赏
  • 举报
回复
SuspendThread The SuspendThread function suspends the specified thread. DWORD SuspendThread( HANDLE hThread // handle to the thread ); Parameters hThread Handle to the thread. Windows NT: The handle must have THREAD_SUSPEND_RESUME access. Return Values If the function succeeds, the return value is the thread's previous suspend count; otherwise, it is 0xFFFFFFFF. To get extended error information, use the GetLastError function. Remarks If the function succeeds, execution of the specified thread is suspended and the thread's suspend count is incremented. Suspending a thread causes the thread to stop executing user-mode (application) code. Each thread has a suspend count (with a maximum value of MAXIMUM_SUSPEND_COUNT). If the suspend count is greater than zero, the thread is suspended; otherwise, the thread is not suspended and is eligible for execution. Calling SuspendThread causes the target thread's suspend count to be incremented. Attempting to increment past the maximum suspend count causes an error without incrementing the count. The ResumeThread function decrements the suspend count of a suspended thread. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also Processes and Threads Overview, Process and Thread Functions, ResumeThread ResumeThread The ResumeThread function decrements a thread's suspend count. When the suspend count is decremented to zero, the execution of the thread is resumed. DWORD ResumeThread( HANDLE hThread // identifies thread to restart ); Parameters hThread Specifies a handle for the thread to be restarted. Windows NT: The handle must have THREAD_SUSPEND_RESUME access to the thread. Return Values If the function succeeds, the return value is the thread's previous suspend count. If the function fails, the return value is 0xFFFFFFFF. To get extended error information, call GetLastError. Remarks The ResumeThread function checks the suspend count of the subject thread. If the suspend count is 0, the thread is not currently suspended. Otherwise, the subject thread's suspend count is decremented. If the resulting value is 0, then the execution of the subject thread is resumed. If the return value is 0, the specified thread was not suspended. If the return value is 1, the specified thread was suspended but was restarted. If the return value is greater than 1, the specified thread is still suspended. Note that while reporting debug events, all threads within the reporting process are frozen. Debuggers are expected to use the SuspendThread and ResumeThread functions to limit the set of threads that can execute within a process. By suspending all threads in a process except for the one reporting a debug event, it is possible to "single step" a single thread. The other threads are not released by a continue operation if they are suspended. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also Processes and Threads Overview, Process and Thread Functions, SuspendThread
不要做咸鱼 2014-04-28
  • 打赏
  • 举报
回复
不要只拷贝啊,看看参数是干嘛使的,没有定义可以自己定义啊
buyong 2014-04-27
  • 打赏
  • 举报
回复
引用 2 楼 u012507601 的回复:
[quote=引用 1 楼 buyong 的回复:] 假设你可以加一个按钮,然后按下能输出hello world的提示框。 这样你就把上面创建线程的加入start按钮的响应函数里。
其实我之前就试过按照那种方法,但是提示“'pParam' : undeclared identifier”,我不知道咋解决![/quote] pParam是你要传给线程的参数,如果没有,直接写null
小松风行 2014-04-27
  • 打赏
  • 举报
回复
引用 1 楼 buyong 的回复:
假设你可以加一个按钮,然后按下能输出hello world的提示框。 这样你就把上面创建线程的加入start按钮的响应函数里。
其实我之前就试过按照那种方法,但是提示“'pParam' : undeclared identifier”,我不知道咋解决!
buyong 2014-04-27
  • 打赏
  • 举报
回复
假设你可以加一个按钮,然后按下能输出hello world的提示框。 这样你就把上面创建线程的加入start按钮的响应函数里。

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧