Solved

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

Posted on 2008-06-11
6
1,148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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