?
Solved

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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

765 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