473,783 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asynchronous Form.ShowDialog ()

Enclosed below is a class that contains one member item called _frm.
It is just a standard System.Windows. Forms.Form class defined elsewhere
(just disregard the definition of the object). My question is if I
want to start a System.Windows. Forms.Form via ShowDialog in another
thread what is the best way to do it?
Basically, I want to start a form, then continue executing elsewhere
and occasionally posting data to the form. I know you could do a
ShowDialog and then spawn off another thread inside of the Form class
to do your processing, but I do not want to do it that way. I
understand that I'll have to use Invoke to update the Controls on the
form from the different threads. I am just interested in knowing if
these two ways are the only way I can do it. Start1 and Start2 are the
two separate methods. Also could someone explain why I need to do an
Application.DoE vents(); in method number 1? If I don't _frm's dialog
hangs. Thanks.

using System;
using System.Threadin g;
using System.Windows. Forms;

namespace testns
{
public class MyClass
{
private Form _frm;

public MyClass()
{}

public void Start1( )
{
_frm = new Form( );
Thread th = new Thread( new ThreadStart(
ThreadFunc1 ) );
th.Start( );
while ( _frm.DialogResu lt !=
DialogResult.Ca ncel )
{
Application.DoE vents( ); // why is this necessary in this
function?
// do whatever work is needed
}
}

public void Start2( )
{
Thread th = new Thread( new ThreadStart(
ThreadFunc2 ) );
th.Start( );
while ( _frm.DialogResu lt !=
DialogResult.Ca ncel )
{
// do whatever work is needed
}
}

private void ThreadFunc1( )
{
if ( _frm != null )
{
_frm.ShowDialog ( );
}
}

private void ThreadFunc2( )
{
_frm = new Form( );
_frm.ShowDialog ( );
}
}
}

Nov 16 '05 #1
1 19072
Application.DoE vents is needed because you are in a tight loop, meaning the
thread cannot process any UI events. Application.DoE vents allows your code to
interrupt the looping thread and process the Windows messages currently in
the message queue, giving the impression of responsiveness. Generally (if not
always), if you are using this method, you have badly designed code.

I would suggest http://www.yoda.arachsys.com/csharp/threads/ as a good
reference for mutlithreaded coding techniques.

"NanoWizard " wrote:
Enclosed below is a class that contains one member item called _frm.
It is just a standard System.Windows. Forms.Form class defined elsewhere
(just disregard the definition of the object). My question is if I
want to start a System.Windows. Forms.Form via ShowDialog in another
thread what is the best way to do it?
Basically, I want to start a form, then continue executing elsewhere
and occasionally posting data to the form. I know you could do a
ShowDialog and then spawn off another thread inside of the Form class
to do your processing, but I do not want to do it that way. I
understand that I'll have to use Invoke to update the Controls on the
form from the different threads. I am just interested in knowing if
these two ways are the only way I can do it. Start1 and Start2 are the
two separate methods. Also could someone explain why I need to do an
Application.DoE vents(); in method number 1? If I don't _frm's dialog
hangs. Thanks.

using System;
using System.Threadin g;
using System.Windows. Forms;

namespace testns
{
public class MyClass
{
private Form _frm;

public MyClass()
{}

public void Start1( )
{
_frm = new Form( );
Thread th = new Thread( new ThreadStart(
ThreadFunc1 ) );
th.Start( );
while ( _frm.DialogResu lt !=
DialogResult.Ca ncel )
{
Application.DoE vents( ); // why is this necessary in this
function?
// do whatever work is needed
}
}

public void Start2( )
{
Thread th = new Thread( new ThreadStart(
ThreadFunc2 ) );
th.Start( );
while ( _frm.DialogResu lt !=
DialogResult.Ca ncel )
{
// do whatever work is needed
}
}

private void ThreadFunc1( )
{
if ( _frm != null )
{
_frm.ShowDialog ( );
}
}

private void ThreadFunc2( )
{
_frm = new Form( );
_frm.ShowDialog ( );
}
}
}

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10012
by: Paul Aspinall | last post by:
Can anyone advise how to display a form on top, and modal, without using ShowDialog?? Thanks
4
9101
by: C M Shaw | last post by:
I have a form which I want to show modally; it's a fairly old form that's been ported up several versions of VB, and I'd like to keep its rewriting to a minimum. Basically, it is used in this sequence: 1. The form is shown. The Form_Load event does some initialization. 2. Further parameters are passed to this form. 3. We actually need this form to be modal, so we hide it and show it again modally. 4. Stuff happens on the form based...
4
4394
by: Dennis Sjogren | last post by:
Greetings! First, I'm not 100% sure where to post this question. I use VB.NET for this project, but it's really a design question (a question on which method to use when solving this problem). In this medium sized (30 or so forms) application, our users have requested a more visual notification of when the client (this app) is communicating with the server, or for other lenghty processes. Disabling the current form and changing the...
4
5230
by: trialproduct2004 | last post by:
Hi all I am new to vb.net application. I am using showdialog property of form to display form. First time it is working properly. But next time it is giving me error that ' object reference not set to an instance of an object'. When i went through stacktrace, i found that it is giving error in showwindow. I am not getting why it is happening.
7
5231
by: Boni | last post by:
Dear all, if I do myform.close() is it disposed or not? is it correct do following? myForm.close() Info=SomeControlOntheForm.text This seems to work. But am not sure that it is waterproof. Thanks, Boni
5
10908
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I have a form that has a button. ( this form is a child form of a parent form ( main form ). Anway...in this child form I have a button, and if clicked a bunch of code will get executed. I would like to show a Progress Bar / form in modal/ShowDialog...
0
1757
by: Bishoy George | last post by:
Hi, I have a asp.net 2.0 web application. I want to implement the asynchronous model through http handler in web.config ------------------------------------------------------------------------------------------------------------------------- My web.config file: ---------------------
1
2324
by: Frank Rizzo | last post by:
I am not grokking the difference between Form.ShowDialog() and Form.ShowDialog(this). I have a form (parent form) that kicks off a modal dialog using Form.ShowDialog(). The modal dialog has a 3rd party control on it. The weird thing is that I can click on parent form's icon in the taskbar and it will bring up the parent form (obscuring the modal form). I can't do anything on the parent form (e.g. it does not take mouse or keyboard...
3
4694
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The form that has the System.Windows.Forms.LinkLabel controls on it is in a different project and under a different namespace from the file where the LinkLabel_LinkClicked events are, so I can't just do frm.ShowDialog under the LinkClicked method. ...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.