?
Solved

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

Posted on 2008-06-11
6
Medium Priority
?
1,179 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
5 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 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 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