简体   繁体   English

在C ++中的UI线程中需要帮助

[英]need help in UI threads in C++

I'm using UI threads and I built one thread with message map and it works fine, the problem is when I'm tring to create another thread from the first one. 我正在使用UI线程,并使用消息映射构建了一个线程,它工作正常,问题是当我尝试从第一个线程创建另一个线程时。

When I'm getting to this line: 当我到达这一行时:

this->PostThreadMessage(WM_MYTHREADMESSAGE,0,0);

I'm getting the next message: 我收到下一条消息:

"No symbols are loaded for any call stack frame. The source code cannot be displayed" “没有为任何调用堆栈框架加载任何符号。无法显示源代码”

I dont know if its could be the reason for the problem but I have built two message maps, one for each thread, I don't know if its ok to do so. 我不知道它是否可能是问题的原因,但是我已经建立了两个消息映射,每个线程一个,不知道这样做是否可行。

The question is difficult to understand. 这个问题很难理解。 I'm assuming that you're stepping through your program in the debugger, and you get to that PostThreadMessage line. 我假设您正在调试器中逐步执行程序,然后进入PostThreadMessage行。

If you choose Step Into, the debugger will try to step into the PostThreadMessage call (or the framework wrapper, depending on the type of this ). 如果选择单步执行,调试器将尝试单步执行PostThreadMessage调用(或框架包装器,具体取决于this的类型)。 Since PostThreadMessage is a system call, it's likely you don't have symbols for that code. 由于PostThreadMessage是系统调用,因此您可能没有该代码的符号。 The debugger will just show you disassembly. 调试器只会向您显示反汇编。 You can try to use the Microsoft symbol server, but I don't see much point in trying to trace into PostThreadMessage . 您可以尝试使用Microsoft符号服务器,但是尝试跟踪PostThreadMessage并没有多大意义。 If the parameters are right, it's going to post the message to the specified thread's queue. 如果参数正确,它将把消息发布到指定线程的队列中。 Not much to see there. 看不到那里。

Posting message to other threads is tricky business. 将消息发布到其他线程是一件棘手的事情。 Most Windows programs, even multithreaded ones, typically keep all the UI work to a single thread. 大多数Windows程序,甚至是多线程程序,通常都将所有UI工作保持在一个线程中。 It can be done, but there are a lot of caveats and it's usually not worth the pain. 可以做到,但是有很多警告,通常不值得付出痛苦。

So there are couple of things: 因此,有几件事:

  1. if you want to notify the UI thread from the worker thread, then you should not use PostThreadMessage , here is why . 如果要从工作线程通知UI线程,则不应使用PostThreadMessage这就是为什么
  2. When this->PostThreadMessage(...) called in a member function of thread A, the message will be sent to thread A, because this points to CWinThread of A. You have to get a pointer to the other thread to post a message to it. 在线程A的成员函数中调用this->PostThreadMessage(...) ,该消息将发送到线程A,因为this指向A的CWinThread 。您必须获得指向另一个线程的指针才能发布消息。对此。
  3. Finally if you want to notify your UI thread, use PostMessage to send a message to the window created by that thread. 最后,如果您想通知UI线程,请使用PostMessage将消息发送到该线程创建的窗口。 Add a corresponding handler to the window message map. 将相应的处理程序添加到窗口消息映射。

Hope this helps 希望这可以帮助

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

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