Example just written, it is hair. .
Until
http://www.lostinet.com/temp/asyncexample.txt
Get the code.
http://www.lostinet.com/temp/asyncexample.exe
Run the program
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Using system.data;
Using system.threading;
Namespace asyncexample
{
///
/// Form1 summary description.
/// summary>
Public Class Form1: System.Windows.Forms.form
{
Private system.windows.forms.groupbox groupbox1;
Private system.windows.Forms.TextBox textBox1;
Private system.windows.Forms.TextBox textBox2;
Private system.windows.Forms.Label label1;
Private system.windows.Forms.Label label2;
Private system.windows.Forms.Button button1;
Private system.windows.Forms.Button Button2;
Private system.componentmodel.icontainer components;
Private system.windows.Forms.Button Button3;
PRIVATE thread thethread;
Public Form1 ()
{
//
// Windows Form Designer Support
//
InitializationComponent ();
//
// Todo: Add any constructor code after INITIALIZECOMPONENT call
//
// Record the current thread, and later know if a function is executed in other threads.
THREAD = thread.currentthread;
}
///
/// Clean all the 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
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
THIS.GroupBox1 = new system.windows.Forms.groupbox ();
This.TextBox1 = new system.windows.Forms.TextBox ();
This.TextBox2 = new system.windows.Forms.TextBox ();
THIS.LABEL1 = New System.windows.Forms.label ();
THISTEM.WINDOWS.FORMS.LABEL (); this.button1 = new system.windows.forms.button ();
This.Button2 = new system.windows.Forms.Button ();
This.Button3 = new system.windows.Forms.Button ();
THISPBOX1.SUSPENDLAYOUT ();
THIS.SUSPENDLAYOUT ();
//
// Groupbox1
//
This.GroupBox1.controls.addrange (new system.windows.forms.control [] {
this.button3,
this.button2,
THIS.LABEL2,
THIS.LABEL1,
THIS.TEXTBOX2,
this.TextBox1,
THIS.BUTTON1});
this.groupbox1.name = "groupbox1";
this.groupbox1.size = new system.drawing.size (360, 128);
this.groupbox1.tabindex = 0;
THIS.GroupBox1.tabstop = false;
THIS.GroupBox1.text = "Enter two numbers";
//
// textbox1
//
This.TextBox1.Location = new system.drawing.point (56, 24);
THIS.TEXTBOX1.NAME = "TextBox1";
this.TextBox1.tabindex = 0;
THIS.TEXTBOX1.TEXT = "111";
//
// textbox2
//
This.TextBox2.Location = new system.drawing.point (56, 56);
THIS.TEXTBOX2.NAME = "TextBox2";
this.TextBox2.tabindex = 1;
THIS.TEXTBOX2.TEXT = "345";
//
// label1
//
THIS.Label1.Location = new system.drawing.point (8, 24);
THIS.LABEL1.NAME = "label1";
THIS.Label1.size = new system.drawing.size (40, 23);
THIS.Label1.tabindex = 2;
THIS.Label1.Text = "Number 1";
//
// label2
//
This.Label2.Location = new system.drawing.point (8, 56);
THIS.LABEL2.NAME = "label2";
THIS.Label2.size = new system.drawing.size (40, 23);
THIS.Label2.tabindex = 3;
THIS.Label2.Text = "Number 2";
//
// Button1
//
This.Button1.Location = new system.drawing.point (192, 24);
This.button1.name = "button1";
This.button1.size = new system.drawing.size (144, 23); this.button1.tabindex = 1;
This.Button1.text = "Use Callback";
This.Button1.click = new system.eventhandler (this.button1_click);
//
// Button2
//
This.Button2.Location = new system.drawing.point (192, 56);
This.Button2.name = "button2";
This.button2.size = new system.drawing.size (144, 23);
this.button2.tabindex = 4;
This.button2.text = "Use endinvoke";
This.button2.click = new system.eventhandler (this.button2_click);
//
// Button3
//
This.Button3.Location = new system.drawing.point (192, 88);
This.button3.name = "button3";
This.button3.size = new system.drawing.size (144, 23);
This.button3.tabindex = 5;
This.Button3.Text = "Detect endInvoke";
This.Button3.click = new system.eventhandler (this.button3_click);
//
// Form1
//
THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);
THIS.CLIENTSIZE = New System.drawing.size (360, 133);
this.controls.addrange (new system.windows.forms.control] {
THIS.GROUPBOX1});
THIS.NAME = "Form1";
THIS.TEXT = "Form1";
This.GroupBox1.ResumeLayout (false);
This.ResumeLayout (false);
}
#ndregion
///
/// The main entry point for the application.
/// summary>
[Stathread]
Static void main ()
{
Application.run (New Form1 ());
}
Private Bool GetNumBers (Out Double X, Out Double Y)
{
Try
{
X = double.parse (TextBox1.text);
Y = double.parse (TextBox2.text);
Return True;
}
Catch (Exception)
{
X = 0;
y = 0;
Return False;
}
}
PRIVATE DELEGATE DOUBLE (Double X, Double Y);
///
/// This method is the method of this time asynchronous
/// Because it is a method as a process, you can access the properties of Form1
/// summary> /// param>
/// param>
///
Private Double except (Double A, Double B)
{
/ / Get the method to perform the thread
Thread thisthread = thread.currentthread;
// In this example, it will never be established. Method is always executed in other threads
IF (Thisthread == this.THETHREAD)
Throw (New Exception "))))));
MessageBox.show (DateTime.now.tolongTimeString () "Press OK to calculate the result of" "/" b ");
// If b is 0, then you will throw an exception
// will have an explanation of an exception to process asynchronous calls
Return A / B;
}
#Region Callback Call Call
///
/// Use callback to process asynchronous calls
/// summary>
/// param>
/// param>
Private void Button1_Click (Object Sender, System.Eventargs E)
{
Double A, B;
IF (! GetNumBers (Out A, Out B))
{
Messagebox.show ("Please enter the correct number");
Return;
}
// You can re-"." Take a look at whether there is a method that is beginivken is displayed in the list?
// The definition is not, so the IDE is not displayed.
/ / But the compiler helps us get this method, so you can compile
IASYNCRESULT IAR = NEW delegate (except). SeginInvoke (A, B, New AsyncCallback ")," Use Callback ");
/ / About BeginInvoke
// New delegate (except) this sentence does not have to say more.
// NEW is delegated (except). BeginInvoke is a function that is automatically generated by the compiler.
// (A, B, New AsyncCallback "Hello :)")
// a, b is the definition of the delegate is consistent
/ / Then add asyncCallback Callback, Object AsyncContext
// Look at the signature of BeginInvoke on MSDN. .
}
Private Void to remove the completion call (IASYNCRESULT IAR)
{
// This is right. However, I can't find a document explanation is always right.
System.Runtime.Remoting.MESSAGING.ASYNCRESULT AR = (System.Runtime.Remoting.MESSAGING.ASYNCRESULT) IAR;
// Get the entrustment of the new NEW (except)
Delegate D = (except for delegate) ar.asyncdelegate;
Double res;
Thread thisthread = thread.currentthread;
String msg = tristhread == this.tHethread? "/ n This thread and main thread are consistent": "/ n This thread and the main thread are different;
{
// and the beginning of the BeginInvoke
Res = D.Endinvoke (IAR);
// endInvoke only needs to put an IASYNCRESULT to make the end call logo is enough.
/ / Since the definition of the delegation, only the in parameter, so there is no other parameters.
// Look at the signature of BeginInvoke on MSDN. .
Messagebox.show ("Okay, in" In addition to the completion call () ", the result has been obtained: / n" RES MSG);
}
Catch (Exception X)
{
Messagebox.show ("Okay, in" In addition to the call () ", the exception is detected in this method: / n" x.Message MSG);
}
}
#ndregion
#Region uses endinvoke
///
/// This is a container used to store IASYNCRESULT
/// summary>
ArrayList Al = New ArrayList ();
///
/// Use endInvoke to process asynchronous calls
/// Execute this, press OK to handle the newly popped MessageBox
/ / Then press "Detect EndInvoke" to see the result. .
/// summary>
/// param>
/// param>
Private void button2_click (Object Sender, System.Eventargs E)
{
Double A, B;
IF (! GetNumBers (Out A, Out B))
{
Messagebox.show ("Please enter the correct number");
Return;
}
/ / This and the most different from the above call is NULL in the parameter asyncCallback
IASYNCRESULT IAR = New delegate (except). SeginInvoke (A, B, NULL, "Do not use the Iar.isCompleted" directly using Callback, "
Al.Add (IAR);
}
// Test See if there is any stuff is already completed. .
Private void button3_click (Object Sender, System.Eventargs E)
{
String msgs = "Ok," in "Button3_Click () detection endInvoke" method: ";
Thread thisthread = thread.currentthread;
String msg = tristhread == this.ththread? "/ n This thread and main thread are consistent": "/ n This thread and the main thread are different;
MSGS = Msg "/ N / N";
INT countmax = al.count;
INT count = 0;
ArrayList aldelete = new arraylist ();
Foreach (IASYNCRESULT IAR IN AL)
{
// If you don't finish it, let's take a look at the next one.
if (! i.iscompleted) Continue; // is okay. . The call of this IAR representative is completed.
COUNT ;
// I can't modify Al, so I have this way.
Aldelete.Add (IAR);
// is the same as the usage above.
System.Runtime.Remoting.MESSAGING.ASYNCRESULT AR = (System.Runtime.Remoting.MESSAGING.ASYNCRESULT) IAR;
Delegate D = (except for delegate) ar.asyncdelegate;
Double res;
Try
{
//
Res = D.Endinvoke (IAR);
MSGS = (String) a.nessncstate "/ has obtained results: / n" RES "/ N / N";
}
Catch (Exception X)
{
Msgs = (String) a.asyncState "/ n detection exception: / n" x.Message "/ n / n";
}
}
// Remove IASYNCRESULT that has been successfully completed from Al
FOREACH (IASYNCRESULT IAR IN ALDELETE)
{
Al.Remove (IAR);
}
IF (countmax> 0 && count == 0)
Msgs = "Pour, there are still few completed."
IF (countmax == 0)
Msgs = "There is no call ..";
Messagebox.show (MSGS);
}
#ndregion
}
}