Solved

Is it safe to open each C#.Net WinForm in a separate thread?

Posted on 2008-06-11
6
1,114 Views
Last Modified: 2008-07-21
The real problem I am trying to solve is that I would like all modal dialog boxes to be modal only to their parent form.  In C#.Net, however, all modal dialog boxes (using ShowDialog) are modal to the entire thread.  There are, therefore two possible solutions to this problem: 1) mimic ShowDialog() by using Show() and disabling the parent a different way, or 2) open each WinForm in a separate thread so that any modal dialog boxes only lock that one form.

Option #1 was explored in question #23378010, but I'd like to explore the other option (one thread per form) with this question.  It will take some work to make our application thread-safe, so I'd like to know first if this is even a viable option.

Initial testing of this option seems to work ok, except that I will occasionally get a 'System.OutOfMemoryException Error creating window handle at System.Windows.Forms.NativeWindow.CreateHandle' exception when I try to open several windows in rapid succession ( I do not get the same exception if I open the same number of windows slowly).

We are using C# and .Net 1.1

Does anyone have experience with opening different forms in different threads?
0
Comment
Question by:tjw_bld
  • 3
  • 2
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21762708
I have never tryed, but you can try to lauch the dialog with:

yourForm.ShowDialog(theRealParent);
0
 

Author Comment

by:tjw_bld
ID: 21762779
Actaully,that is the version of ShowDialog that we currently use.  Calling yourForm.ShowDialog(theRealParent) will set the owner of the dialog box to be theRealParent, but all forms for the current thread will still be locked, not just theRealParent.  Both versions of ShowDialog work the same as far as locking all forms of the current thread.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 125 total points
ID: 21762971
a thread can manage just 1 modal dialog, indeed can manage just 1 dialog of any kind at a time
so, using differente threads will allow you to handle them virtually simultaneously.
0
 

Accepted Solution

by:
tjw_bld earned 0 total points
ID: 21825487
When searching the internet, you will come across several posts that indicate the you cannot do this.  For example "Only the main thread can access the user interface (forms, etc).  This prevents many race conditions" from http://ginger.cs.uwp.edu/Classes/Cs370/homework/NETCThreads.doc.

jaime_olivares' post above seems to indicate that you can do this.  Plus, just the fact that .Net provides a method Application.ExitThread seems to indicate that you should be able to do this.

However, when I try it in my code, I get various exceptions if multiple windows are opened at roughly the same time, although this may be due to some third-party libraries we are using.

As my original question indicates, I have also been exploring the option of mimicing the ShowDialog method in question #23378010.  In fact, I have finally gotten that solution to work and I will post the code in that question shortly.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21849261
no objection
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now