MFC和模式消息框. [英] MFC and modal message boxes.

查看:86
本文介绍了MFC和模式消息框.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在过去的几天里,这件事使我发疯,所以我将不胜感激.

我在VS2008中编写MFC对话应用程序.

简而言之,即时通讯正在执行以下操作.

我有一个属于*dlg类成员的函数(我们称其为MyAppDlg::PostMessageToDB()).

我创建了一个计时器和一个MyAppDlg::onTimer()处理程序方法,只要计时器打勾,该方法就会被调用.

我还创建了一个MSMQ COM对象,并用MyAppDlg::newMessage()函数注册了它的"Arrived"事件.当新消息到达MSMQ队列时,将调用此函数.

这两个函数都调用PostMessageToDB()函数.

如果出现错误(例如与数据库无连接),则PostMessageToDB()函数会通过调用AfxMessageBox( errorMsg, MB_RETRYCANCEL |MB_ICONERROR| MB_SYSTEMMODAL);方法弹出错误消息.

这种方法是模态的. PostMessageToDB()函数的执行冻结,并且直到用户单击重试"(或取消")按钮,我的应用程序才继续执行.

到目前为止一切都很好.

但是,在等待用户响应的同时,如果计时器再次计时或收到新消息,我的应用程序将被唤醒!调用了相应的函数,似乎程序的新实例"开始了其执行流程.结果是再次遇到相同的错误并弹出另一条错误消息.

这怎么可能呢?
是否有任何隐藏线程参与其中?
如果在上述流程的另一个实例"正在执行时,如果我按重试按钮,将会发生什么?

据我所知,在我的应用程序完成对前一条消息的处理并返回之前,不应调度新事件(计时器滴答声或队列中的新消息).这就是Windows消息循环正常工作的方式吗?

我想我想念MFC体系结构的基础知识.如果您知道可以帮助我了解上述行为的教程/文章的链接,我将不胜感激!

Hello to everyone,

This thing is driving me crazy for the past days, so i would appreciate any insight.

Im writing an MFC dialogue application in VS2008.

In simple words im doing the following.

I have a function that is a member of the *dlg class (lets call it MyAppDlg::PostMessageToDB()).

I have created a timer and an MyAppDlg::onTimer() handler method that gets called whenever the timer ticks.

I have also created an MSMQ COM object and have registered its "Arrived" event with the MyAppDlg::newMessage() function. This function gets called when a new message arrives in the MSMQ queue.

Both these functions call the PostMessageToDB() function.

If theres an error (e.g. no connectivity with the DB) the PostMessageToDB() function pops an error message by calling the AfxMessageBox( errorMsg, MB_RETRYCANCEL |MB_ICONERROR| MB_SYSTEMMODAL); method.

This method is modal. The execution of the PostMessageToDB() function freezes and my application does not continue until the user hits the retry (or cancel) button.

So far so good.

BUT while waiting for a user response, if the timer ticks again or a new message arrives, my application gets woken up! The corresponding functions are called and it seems like a new "instance" of my program starts its execution flow. The result is to reach again at the same error and pop another error message.

How can this ever be possible?
Is anywhere there a hidden thread involved?
What will happen if i press the retry button while another "instance" of the above flow is on its way?

To what i know, the new event (timer tick or new message in the queue) shouldn''t be dispatched before my application completes the processing of the previous message and returns. That''s how the windows message loop works right?

I guess i miss the basics of the MFC architecture. If you know a link to a tutorial/article that can help me understand the above behavior i would be grateful!

推荐答案

消息框调用似乎来自新线程.这使您的主线程保持响应状态,从而能够处理新消息.在这种情况下,最好避免使用消息框,更好的主意是使用带有列表框的无模式对话框,该对话框显示正在运行的错误日志.

或者,请确保同步线程并停止处理新消息,直到用户关闭消息框为止.

至于发生这种情况的原因或方式,我猜想是在新线程上(从线程池中)触发了MSMQ事件.
It seems likely that the message box calls are from new threads. This leaves your main thread responsive and thus able to process new messages. In such a scenario it''s best to avoid using a message box, and a better idea may be to use a modeless dialog with a listbox that shows a running log of errors.

Alternatively, make sure you sync the threads and stop processing new messages until the user has dismissed the messagebox.

As to why or how this happens I''d guess that the MSMQ events are fired on new threads (from a thread pool).


如果您使用SEH结构化异常处理,则可能具有中断到消息循环(是的,它可以做到).据我所知,这只会在Windows mobile/ce上发生,但从未测试过.关闭编译器上的SEH,然后重试.

使用spy ++观看消息.

只需编写自己的消息泵,即可编写自己的模型对话框,错误将显而易见.它的代码行并不多.如果您想正确处理计时器消息,您将最终自己编写它,因为模型对话框和计时器不是朋友.如果我记得,通常计时器消息不会通过(被丢弃),但这可能取决于注册的窗口的类型,并且类型太多.
if your using SEH structured exception handling, it may have broken off to the message loop ( yes it can do that). That will only happen on windows mobile/ce to my knowledge but never tested it. Turn off SEH on the compiler and try it again.

Use spy++ to watch the messages.

Write your own model dialog by just writing your own message pump and the error will be obvious. It''s not many lines of code. If you want to handle timer messages properly you''ll end up writing it yourself as model dialogs and timers are not friends. Generally timer messages don''t get through ( they are chucked) if I remember but it may depend on the type of window registered and there are so many types.


这篇关于MFC和模式消息框.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆