Solved

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

Posted on 2008-06-11
6
1,126 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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