1

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 whole 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)
AZ.
  • 7,333
  • 6
  • 44
  • 62

2 Answers2

2

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().

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I just found out that if my form is shown using ShowDialog(), everything is working! So what is "Form.ShowDialog()" doing? Is that forcing the application to pump the message loop on the .net Form? – AZ. Sep 29 '11 at 23:48
  • 1
    ShowDialog pumps a message loop. To make the dialog modal. The Winforms message loop. – Hans Passant Sep 29 '11 at 23:52
  • @AZ. it may be "working" in the sense that Idle is fired, but the Idleness triggering the event has nothing to do with Idleness in your main MFC application. – Aidan Ryan Sep 30 '11 at 00:22
  • That's debatable, its windows are disabled by the ShowDialog() call. That's pretty idle. – Hans Passant Sep 30 '11 at 00:27
  • So what if I show the form using Form.Show()? Is there any WinForms message loop created? I'm asking this because as I dig deeper in the source code I find the control uses ProcessDialogKey() and it's never get called?! – AZ. Sep 30 '11 at 00:40
  • Asked and answered. Mentioned the keyboard processing trouble too. – Hans Passant Sep 30 '11 at 00:46
1

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • so you mean Application.Idle is never called? And the whole Application class is useless when used in MFC? – AZ. Sep 29 '11 at 22:43
  • 1
    it is not useless but not used in a designed way since you are usingthe MFC message pump, not the one in Application.Run. If you want to raise Application.Idle, hook MFC's idle processing and raised the event by yourself – Sheng Jiang 蒋晟 Sep 29 '11 at 23:38