简体   繁体   English

MFC:从工作线程向主线程发送消息,以停止工作线程

[英]MFC: sending a msg from worker thread to the Main thread to stop the worker thread

I am fixing a GUI application programmed with MFC. 我正在修复用MFC编程的GUI应用程序。 To run a communication stack a new thread is created. 为了运行通信堆栈,创建了一个新线程。 On some events I want to stop this but I know that only in the worker thread and I cannot get the correct behaviour from stopping the worker thread from itself. 在某些情况下,我想停止此操作,但我知道仅在工作线程中,并且无法从其自身停止工作线程来获得正确的行为。 So I need to send a message to the main thread and tell it to stop the worker thread so that the main thread can then take the correct action. 因此,我需要向主线程发送一条消息,并告诉它停止工作线程,以便主线程可以采取正确的操作。

I am unsure how to do this. 我不确定该怎么做。 I have looked at the SendMessage function in CWnd but it does not take threadid as an argument and I dont get what I need to include to use http://msdn.microsoft.com/en-us/library/windows/desktop/ms644944(v=vs.85).aspx . 我已经看过CWnd中的SendMessage函数,但是它没有将threadid作为参数,并且我没有得到使用http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms644944所需要包含的内容(v = vs.85).aspx

Actually, the only safe way to do anything to a worker thread is from within the worker thread itself. 实际上,对工作线程执行任何操作的唯一安全方法是从工作线程本身内部进行。 It should probably be structured as a while(bRun) loop so it runs until the program is shutting down. 它可能应该被构造为while(bRun)循环,以便它运行直到程序关闭。 Whatever you are trying to do (stop and start) should be handled by code in the while loop. 无论您想做什么(停止和开始),都应由while循环中的代码处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM