PC Review


Reply
Thread Tools Rate Thread

Asynchronous Form.ShowDialog()

 
 
NanoWizard
Guest
Posts: n/a
 
      29th Dec 2004
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( );
}
}
}

 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGFuIEtlbGxleQ==?=
Guest
Posts: n/a
 
      30th Dec 2004
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( );
> }
> }
> }
>
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
display a clean new form - form.ShowDialog() Patino Microsoft Dot NET Compact Framework 1 18th Apr 2006 11:15 PM
ShowDialog and asynchronous code execution Dennis Sjogren Microsoft VB .NET 4 13th Oct 2004 12:02 PM
Re: Form.ShowDialog Klaus Löffelmann Microsoft VB .NET 0 10th Mar 2004 08:44 PM
Re: Form.ShowDialog Tom Leylan Microsoft VB .NET 0 10th Mar 2004 08:41 PM
Form.ShowDialog() / ShowDialog(IWin32Window) peshrad Microsoft C# .NET 2 28th Jan 2004 02:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 PM.