Application.Run vs Form.Show vs Form.ShowDialog

What does this new addition to the basic C# project do exactly I'm having major problems with my existing code to do with cross-thread operations and also an intermittent bug in the debugger. I can't seem to get any questions answered about either problem.

Does this Application.Run statement perform some new fundamental operation I am unaware of

To demonstrate the bug, simply create a new WindowsApplication and replace Application.Run(new Form1) with Form1 f = new Form1, Form1.Show(). Instant intermittent crashes.

Could someone point me to a demonstration of how to run multiple independent Forms under the new framework maybe


Answer this question

Application.Run vs Form.Show vs Form.ShowDialog

  • ndemaster

    Hmm... Application.Run runs the message loop so you cannot simply replace it with Form1.Show(). Anyway I've tried it and it does not crash, it just exits instantly.

    Adding Application.Run() after form1.Show() seems to work OK except the fact that the application does not exit when you close the form (that's another purpose of Application.Run(form1), to exit the application when the main window closes).

    Regarding cross-thread operations you probably access a form/control from another thread than the thread that created it which is not correct.

  • ColdCold

    Dammit! Smile Today my project too simply exits instantly. I wish I knew what was going on! I'll have to switch back to the x64 OS/IDE and see if its still doing it there too. Ahh yes the message loop of course. I was hoping maybe you could give me some information about 'LoaderLock', yet another new 'bug' introduced by 2005. But if that's all it does that's fine (but where's the comments in the template code these days, tsk tsk!)

    Regarding cross thread operations, I know what they are its just an annoying new feature (basically because I've broken the rule in so many places on my latest project). Some of us are capable of writing threadsafe code! I also think if it was a big issue they could have put synchronising code on the properies of the control classes. I mean, I can't even use a pointer to a window from another thread - that's just a pain, because I have to make sure any window opening happens only in the main window's thread. I can't have a worker thread pop open a dialog box to say its finished, for instance.

    I've finally sussed out all the little caveats of Show vs ShowDialog (and I swear last night it was still crashing at random), the question is why the hell isn't the information in the MSDN docs It doesn't tell you there that, for instance, the only way to get a non-modal 'toolbox' type window that stays on top of the main window at all times is to use Show() and pass in your main window as an argument. Given that I can't do this from another thread, what is the solution then

    As I keep saying, someone from Microsoft needs to sit down and write us an example of how to do all the little tricks with windows that a real application does, without dragging side-topics like MDI and XML and DataSets into the picture.

    It needs to run at least three windows at once, have background worker threads that start and stop and can be controlled, be able to do modal and non-modal dialog boxes spawned out of any of the windows and that disable/enable any combination of the windows (or none of them), and here's a real challenge: a modal dialog box that still allows the poor user to move/resize/minimise the main window in order to achieve what the modal box is asking. I've yet to pull that one off!

    All this really comes down to is a lack of information - I think you're supposed to have been doing this sort of thing since Visual Studio 1.0 or have been swept off your feet since by one of the new technologies, or you've jumped on the bandwagon too late. Maybe its just that the target audience for the API has changed, maybe the Forms API is not sufficient for my needs, aargh I don't know! Tongue Tied

    Anyway, thanks for the info although I wish I were closer to an answer or at least a reason for my problems. It seems one of my annoyances has just 'gone' (for now) so I guess I shouldn't complain. Big Smile

  • Application.Run vs Form.Show vs Form.ShowDialog