开发者

Why does Application.Idle never fire when my form is hosted in a MFC application?

开发者 https://www.devze.com 2023-04-09 17:33 出处:网络
I\'m building a .net plugin for a MFC application. I created a form using WinForms and .net4. The form is shown after user clicks a button in the MFC application. One of my controls uses Application.I

I'm building a .net plugin for a MFC application. I created a form using WinForms and .net4. The form is shown after user clicks a button in the MFC application. One of my controls uses Application.Idle to do some task.

The problem is, it seems Application.Idle is never fired.

  • What exactly does "Application" refer to in this scenario? Is it the whol开发者_如何学Ce MFC application, or the .net form?
  • Why doesn't Application.Idle fire?

UPDATE

I managed to get it work, here are the two possible solutions:

  • show the form using Form.ShowDialog() (thus the form runs its own .net message pump)
  • create a STA thread, create the form on the STA thread and call Application.Run(). Then you can use Form.Show() (the form uses the .net message pump in the thread it was created, in this case: .net STA thread)


The only way that Winforms can figure out that the application is idle is when it doesn't find any other messages on the message queue. Your problem is that it is not Winforms that pumps the message loop, it is MFC. Which doesn't know anything about the Winforms bells and whistles. Other things go wrong, note that tabbing and short-cut keystrokes no longer work, also handled by the Winforms message loop.

There is no clean solution for this, there can be only one master. Very imperfect fixes are using Form.ShowDialog() and starting a new STA thread so that you can call Application.Run().


The .net form is not a .net application, and neither is the MFC application. MFC has its own code that it calls in the application when it's idle; it has no knowledge of Application.Idle.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号