首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

如何在一个线程里给另一个线程发消息

2013-01-21 
怎么在一个线程里给另一个线程发消息?怎么在一个线程里给另一个线程发消息?想实现一个创建进度条的线程A,A

怎么在一个线程里给另一个线程发消息?
怎么在一个线程里给另一个线程发消息?

想实现一个创建进度条的线程A,A线程创建后开启另一个线程B,如果B执行失败则给A发消息,A收到消息后将创建的进度条关闭,该如何实现呢?求指导~

A线程中怎么实现消息处理?B线程怎么给A线程发消息?
刚接触多线程~
[解决办法]
先自定义消息,然后PostMessage
[解决办法]
CWinThread


A CWinThread object represents a thread of execution within an application. The main thread of execution is usually provided by an object derived from CWinApp; CWinApp is derived from CWinThread. Additional CWinThread objects allow multiple threads within a given application.

There are two general types of threads that CWinThread supports: worker threads and user-interface threads. Worker threads have no message pump: for example, a thread that performs background calculations in a spreadsheet application. User-interface threads have a message pump and process messages received from the system. CWinApp and classes derived from it are examples of user-interface threads. Other user-interface threads can also be derived directly from CWinThread.

Objects of class CWinThread typically exist for the duration of the thread. If you wish to modify this behavior, set m_bAutoDelete to FALSE.

The CWinThread class is necessary to make your code and MFC fully thread-safe. Thread-local data used by the framework to maintain thread-specific information is managed by CWinThread objects. Because of this dependence on CWinThread to handle thread-local data, any thread that uses MFC must be created by MFC. For example, a thread created by the run-time function_beginthreadex cannot use any MFC APIs.

To create a thread, call AfxBeginThread. There are two forms, depending on whether you want a worker or user-interface thread. If you want a user-interface thread, pass to AfxBeginThread a pointer to the CRuntimeClass of your CWinThread-derived class. If you want to create a worker thread, pass to AfxBeginThread a pointer to the controlling function and the parameter to the controlling function. For both worker threads and user-interface threads, you can specify optional parameters that modify priority, stack size, creation flags, and security attributes. AfxBeginThread will return a pointer to your new CWinThread object. 

Instead of calling AfxBeginThread, you can construct a CWinThread-derived object and then call CreateThread. This two-stage construction method is useful if you want to reuse the CWinThread object between successive creation and terminations of thread executions.



For more information on CWinThread, see the articlesMultithreading with C++ and MFC,Multithreading: Creating User-Interface Threads,Multithreading: Creating Worker Threads, andMultithreading: How to Use the Synchronization Classes in Visual C++ Programmer’s Guide.

Class Members 
[解决办法]
  Base Class 
[解决办法]
  Hierarchy Chart

Sample   MFC Sample MTGDI MSDN98\SAMPLES\VC98\MFC\ADVANCED\MTGDI\*.*

See Also   CWinApp, CCmdTarget

[解决办法]


[解决办法]
真的很无语啊,诸位乱开药方,我看大部分有关这个论坛的提问,都能在windows核心编程里找到答案。这个问题的处理方法如下:
unsign short callback AThreadProc(void *)
{   
    while (ture)
    {
        MSG msg = {0};
        PeekMessage(.., &msg...);
        swtich (msg.message)
        {
         case WM_QUIT:
              return; // 退出线程
         case ...
        }
    }
    return 0;
}
...   
HANDLE g_hAThread = (HANDLE)_beginthreadex(...., AThreadProc, ....);
...
unsign short callback BThreadProc(void *)
{
    ...
    PostThreadMessage(g_hAThread, .....);
    ...
    return 1;
}

热点排行