473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.DoEvents(); in method number 1? If I don't _frm's dialog
hangs. Thanks.

using System;
using System.Threading;
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.DialogResult !=
DialogResult.Cancel )
{
Application.DoEvents( ); // 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.DialogResult !=
DialogResult.Cancel )
{
// 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 19019
Application.DoEvents is needed because you are in a tight loop, meaning the
thread cannot process any UI events. Application.DoEvents 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.DoEvents(); in method number 1? If I don't _frm's dialog
hangs. Thanks.

using System;
using System.Threading;
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.DialogResult !=
DialogResult.Cancel )
{
Application.DoEvents( ); // 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.DialogResult !=
DialogResult.Cancel )
{
// 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
by: Paul Aspinall | last post by:
Can anyone advise how to display a form on top, and modal, without using ShowDialog?? Thanks
4
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...
4
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). ...
4
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...
7
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....
5
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...
0
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 ...
1
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...
3
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.