You are using a delegate to invoke your method asyncronously, so you are already
using the thread pool. That's the way .BeginInvoke() works. When the code is
dispatched, it is running on a thread pool thread. The thread pool will
automatically throttle down your thread requests if you flood the system with
asynchronous calls. It will create additional threads if it finds that there is
high demand, but it won't create more than the system can handle and it will
start removing threads from the thread pool as demand falls. You don't have to
do anything explicitly to end the thread.

You are required to call EndInvoke() at some point. If you don't, the runtime
will not gaurantee that resources won't leak (although I don't believe it does
today). Calling EndInvoke() is also the only way to find out if a an exception
has been thrown. Any exception thrown by the method called with BeginInvoke()
will have the results thrown when the EndInvoke() is called.

There is one more thing to be aware of when using BeginInvoke() in a forms based
application. You can't call any method on a class derived from
System.Windows.Forms.Control from any thread other than the one that created the
window. That's why InvokeRequired() is exposed on the Control class. It will
return true if the executing thread is anything other than the proper one.

Control also exposes two methods that will solve the problem for you:
BeginInvoke() and Invoke(). BeginInvoke() may sound like it's the same thing as
the BeginInvoke() on a delegate, but it's not. Calling BeginInvoke() on any
class derived from Control (all classes that wrap a win32 window handle do)
works a little different. It will end up invoking the method that is passed in
via the delegate it takes as an argument on the proper thread.

Read up on async calls in this excellant article written by Ted Pattison. It's
written for VB programmers, but there is very little that is specific to VB in
the article.

http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/default.aspx


Regards, Bob

On Wed, 23 Feb 2005 11:55:19 -0500 (EST), "Sathiamurthy, Venkat"
<[EMAIL PROTECTED]> wrote:
> I have a situation, I am calling a method asynchronously, which does not
> return values. If I make 100 call asynchronously, will the performance
> go down?
>
> My understanding is - I can't use threadpool, because FormA  loads FormB
> and which inturn calls a method Asynchronously. FormA may load 100
> FormB, but FormB will call the method only once per instance.
>
> My additional questions are,
> a. I don't have a need to call endinvoke, What will happen to that child
> thread, when it will die?
> b. How do I control the child thread, i.e. terminate the child thread
> created my the asycn call?
>
>
> Thanks,
> Venkat Sathiamurthy, PMP
>
> Please see the below code:
>
> using System;
>
> using System.Drawing;
>
> using System.Collections;
>
> using System.ComponentModel;
>
> using System.Windows.Forms;
>
> using System.Data;
>
> using System.Threading;
>
> namespace TestThreadHold
>
> {
>
> /// <summary>
>
> /// Summary description for Form1.
>
> /// </summary>
>
> public class Form1 : System.Windows.Forms.Form
>
> {
>
> private System.Windows.Forms.Button button1;
>
> /// <summary>
>
> /// Required designer variable.
>
> /// </summary>
>
> private System.ComponentModel.Container components = null;
>
> public Form1()
>
> {
>
> //
>
> // Required for Windows Form Designer support
>
> //
>
> InitializeComponent();
>
> //Thread.CurrentThread.Name ="Master";
>
> //
>
> // TODO: Add any constructor code after InitializeComponent call
>
> //
>
> }
>
> /// <summary>
>
> /// Clean up any resources being used.
>
> /// </summary>
>
> protected override void Dispose( bool disposing )
>
> {
>
> if( disposing )
>
> {
>
> if (components != null)
>
> {
>
> components.Dispose();
>
> }
>
> }
>
> base.Dispose( disposing );
>
> }
>
> #region Windows Form Designer generated code
>
> /// <summary>
>
> /// Required method for Designer support - do not modify
>
> /// the contents of this method with the code editor.
>
> /// </summary>
>
> private void InitializeComponent()
>
> {
>
> this.button1 = new System.Windows.Forms.Button();
>
> this.SuspendLayout();
>
> //
>
> // button1
>
> //
>
> this.button1.Location = new System.Drawing.Point(64, 80);
>
> this.button1.Name = "button1";
>
> this.button1.Size = new System.Drawing.Size(112, 23);
>
> this.button1.TabIndex = 0;
>
> this.button1.Text = "button1";
>
> this.button1.Click += new System.EventHandler(this.button1_Click);
>
> //
>
> // Form1
>
> //
>
> this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
>
> this.ClientSize = new System.Drawing.Size(292, 273);
>
> this.Controls.Add(this.button1);
>
> this.Name = "Form1";
>
> this.Text = "Form1";
>
> this.Load += new System.EventHandler(this.Form1_Load);
>
> this.ResumeLayout(false);
>
> }
>
> #endregion
>
> /// <summary>
>
> /// The main entry point for the application.
>
> /// </summary>
>
> static void Main()
>
> {
>
> Application.Run(new Form1());
>
> }
>
> private void button1_Click(object sender, System.EventArgs e)
>
> {
>
> Form2 frm2 = new Form2 ();
>
> frm2.Show ();
>
> }
>
> private void Form1_Load(object sender, System.EventArgs e)
>
> {
>
> }
>
> }
>
> }
>
>
>
> using System;
>
> using System.Drawing;
>
> using System.Collections;
>
> using System.ComponentModel;
>
> using System.Windows.Forms;
>
> using System.Threading;
>
> namespace TestThreadHold
>
> {
>
> /// <summary>
>
> /// Summary description for Form2.
>
> /// </summary>
>
> public class Form2 : System.Windows.Forms.Form
>
> {
>
> delegate void mydelegate();
>
> private System.Windows.Forms.Button button1;
>
> private System.Windows.Forms.Button button2;
>
> /// <summary>
>
> /// Required designer variable.
>
> /// </summary>
>
> private System.ComponentModel.Container components = null;
>
> public Form2()
>
> {
>
> //
>
> // Required for Windows Form Designer support
>
> //
>
> InitializeComponent();
>
> //
>
> // TODO: Add any constructor code after InitializeComponent call
>
> //
>
> }
>
> /// <summary>
>
> /// Clean up any resources being used.
>
> /// </summary>
>
> protected override void Dispose( bool disposing )
>
> {
>
> if( disposing )
>
> {
>
> if(components != null)
>
> {
>
> components.Dispose();
>
> }
>
> }
>
> base.Dispose( disposing );
>
> }
>
> #region Windows Form Designer generated code
>
> /// <summary>
>
> /// Required method for Designer support - do not modify
>
> /// the contents of this method with the code editor.
>
> /// </summary>
>
> private void InitializeComponent()
>
> {
>
> this.button1 = new System.Windows.Forms.Button();
>
> this.button2 = new System.Windows.Forms.Button();
>
> this.SuspendLayout();
>
> //
>
> // button1
>
> //
>
> this.button1.Location = new System.Drawing.Point(40, 24);
>
> this.button1.Name = "button1";
>
> this.button1.TabIndex = 0;
>
> this.button1.Text = "button1";
>
> //
>
> // button2
>
> //
>
> this.button2.Location = new System.Drawing.Point(40, 64);
>
> this.button2.Name = "button2";
>
> this.button2.TabIndex = 1;
>
> this.button2.Text = "button2";
>
> //
>
> // Form2
>
> //
>
> this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
>
> this.ClientSize = new System.Drawing.Size(292, 273);
>
> this.Controls.Add(this.button2);
>
> this.Controls.Add(this.button1);
>
> this.Name = "Form2";
>
> this.Text = "Form2";
>
> this.Load += new System.EventHandler(this.Form2_Load);
>
> this.ResumeLayout(false);
>
> }
>
> #endregion
>
> private void button1_Click(object sender, System.EventArgs e)
>
> {
>
> MessageBox.Show ("Test1");
>
> }
>
> private void button2_Click(object sender, System.EventArgs e)
>
> {
>
> MessageBox.Show ("Test2");
>
> }
>
> private void Form2_Load(object sender, System.EventArgs e)
>
> {
>
> mydelegate dl=new mydelegate(Form2_EventSubscribe);
>
> dl.BeginInvoke(null,null);
>
> }
>
> private void Form2_EventSubscribe()
>
> {
>
> //Thread.CurrentThread.Name ="Worker Thread" + DateTime.Now.ToString ();
>
> Thread.Sleep (new TimeSpan (0,0,0,5,0));
>
> if("NO CONNECTION"=="NO CONNECTION")
>
> {
>
> this.button1.Click += new System.EventHandler(this.button1_Click);
>
> this.button2.Click += new System.EventHandler(this.button2_Click);
>
> this.MouseDown += new
> System.Windows.Forms.MouseEventHandler(this.Form2_MouseDown);
>
> }
>
> else
>
> {
>
> this.button1.Click += new System.EventHandler(this.button1_Click);
>
> this.button2.Click += new System.EventHandler(this.button2_Click);
>
> MessageBox.Show ("Connection Succeed");
>
> }
>
> }
>
> private void Form2_MouseDown(object sender,
> System.Windows.Forms.MouseEventArgs e)
>
> {
>
> this.Close ();
>
> }
>
> }
>
> }
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ===================================
> This list is hosted by DevelopMentor�  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>
>

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to