我有一个线程,由主界面调用的COM创建,在这个线程里面向主界面发送两个消息,第一次一切正常,消息按照被发送的顺序到达主界面的消息处理函数,但是如果我在主界面重新这个过程,这时两个消息到达主界面的顺序就反了,造成主界面死锁,请问会有些什么原因会导致出现这种情况。
第一次发送:SendNotifyMessage(m_WinHandle, WM_THREADEVENT, (WPARAM)msg1, (LPARAM)msg);
第二次发送:SendNotifyMessage(m_WinHandle, WM_THREADEVENT, (WPARAM)msg2, (LPARAM)msg);

解决方案 »

  1.   

    PostThreadMessage是线程间发送消息的函数吧.
      

  2.   

    PostThreadMessage
    为何两个消息到达的消息反了就会死?得检查是否有同步的问题?
      

  3.   

    你这个已经属于线程间通信了,所以才会让你试试PostThreadMessage 你的同步机制再检查一下看看,从你的描述看只有两个线程交互,而且容易重现,要查出来不会很难。
      

  4.   

    同步问题
    自己调试看就知道了啊, 在WaitXXX后加断点
      

  5.   

    因为第一个消息到达后进行一些操作,然后锁住主界面,等第二个消息到达再解锁让主界面刷新。现在的问题是为什么第二次时消息到达的顺序反了,有没有什么方法可以查看?明明msg1先发送出去的,但是它比msg2要后到达主界面的处理函数
      

  6.   

    The SendNotifyMessage function sends the specified message to a window. If the window was created by the calling thread, SendNotifyMessage calls the window procedure for the window and does not return until the window procedure has processed the message. If the window was created by a different thread, SendNotifyMessage passes the message to the window procedure and returns immediately; it does not wait for the window procedure to finish processing the message. If you send a message in the range below WM_USER to the asynchronous message functions (PostMessage, SendNotifyMessage, and SendMessageCallback), its message parameters can not include pointers. Otherwise, the operation will fail. The functions will return before the receiving thread has had a chance to process the message and the sender will free the memory before it is used. 
    SendNotifyMessage是个异步的消息函数,所以不能用来同步,您不防试试SendMessage()
      

  7.   

    xtaddqqug(王中) ( 二级(初级)) 信誉:81:
    我并不是要同步,只是要求消息的先后顺序不能变,难道说用SendNotifyMessage发消息,并不能确定消息的先后顺序?这种情况它应该进入消息队列呀
      

  8.   

    unicode(衣不如新,人不如故):您好,我建议您看一候捷翻的关于多线程编程的书。我也经历过您所说的事情,但是Windows对先发出的消息并不一定先先应,这个可能与调度策略有关,我们在学操作系统原理或分组通信的时候应该对这个有所理解,您等待的时间长并不意味着您的优先级高,可能刚来的(后到的)先被调度(响应),先来的已经等了一段时间了,可能在多等一个时隙也没关系。
      

  9.   

    怎么说呢,这个设计总体感觉就怪怪的,如果让我来完成LZ的任务,我可能会这样做:
    发一个消息给主界面,主界面响应函数里面WaitForSingleObject(要卡死就这样,不是卡死只是禁止某些操作就用变量控制),这样主界面就被卡死了,自己这个线程完成某些操作后SetEvent对主界面解锁。个人觉得这样更好些……