wxwidgets 和线程
在我的应用程序中,我从 onThread
函数运行 wglGetCurrentDC()
和 wglGetCurrentContext()
(此函数应按此处声明的方式调用 - EVT_THREAD(wxID_ANY,MyCanvas::onThread)
) 在这两种情况下我都得到NULL
。当我不是从 onThread
运行它时,就可以了...... 为了解决这个问题,有什么解决办法——(我必须在从线程获取事件时运行它们!)
正如 Alex 建议的那样,我更改为 wxPostEvent
将事件重定向到主线程,该线程捕获在其 onThread
函数中发生事件。在这个 onThread
函数中,我有 wglGetCurrentDC()
和 wglGetCurrentContext()
调用.. .他们仍然回来null.请解释我做错了什么。以及如何解决他的问题。
In my application I run wglGetCurrentDC()
and wglGetCurrentContext()
from onThread
function
(this function should be called as declared here - EVT_THREAD(wxID_ANY,MyCanvas::onThread)
)
and I get NULL
in both cases. When I run it not from onThread
it is ok…
What is work around in order to solve the problem – (I have to run them when getting event from the thread!)
As Alex suggested I changed to wxPostEvent
to redirect the event to main thread, which catches the event in its onThread
function.In this onThread
function I have wglGetCurrentDC()
and wglGetCurrentContext()
calls ...They still return null.Please explain me what I am doing wrong. And how to solve he problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我误解了,但是您不应该使用 wxGLCanvas 和 wxGLContext 而不是 Windows 特定的代码吗?至少它可能与其他 wxWidget 代码更兼容。
无论如何,从 wglGetCurrentDC 文档,如果当前窗口的 DC 不存在,则该函数返回 NULL。这表明上下文要么以某种方式被破坏,要么您没有从您认为调用它的窗口中调用它(也许是因为您的线程?)。我会重申亚历克斯所说的话;除了主线程之外,不要从任何线程调用 UI 代码。
如果您可以发布一些代码来显示您如何从线程返回,则可能有助于识别问题。您似乎正在从线程中执行 UI 操作,但没有意识到这一点。 (不过,在没有看到任何代码的情况下很难判断。)
Maybe I'm misunderstanding, but should you not be using wxGLCanvas and wxGLContext rather than the windows-specific code? At the very least it's probably more compatible with other wxWidget code.
Anyway, from the wglGetCurrentDC documentation, the function returns NULL if a DC for the current window doesn't exist. This suggests that either the context was destroyed somehow or you're not calling it from the window you think you're calling it from (perhaps because of your threading?). I would reiterate what Alex said; don't call UI code from any thread besides the main one.
If you could post some code showing how you're returning from the thread it might help identify the problem. It seems likely that you're doing UI stuff from the thread and just not realizing it. (Hard to tell without seeing any code, though.)
不要从工作线程中触及任何与 UI 相关的内容。这是所有 UI 框架的一般要求。使用 wxPostEvent 将工作重定向到主应用程序线程。
Don't touch any UI-related stuff from a worker thread. This is general requirement for all UI frameworks. Use wxPostEvent to redirect a work to the main application thread.