In general, the function is synchronous, that is, the caller must wait until the calling function is executed, and the recipient's subsequent code will continue. For some functions, it is possible to perform a long time, so that the caller is equal to its execution, it may make the user feel that the program hangs. Therefore, there is often a need to provide an asynchronous method that allows the caller to continue the subsequent code immediately after the call. Using the Delegate mechanism provided by the .NET Framework, it is easy to override a very long function to a way to support asynchronous. Below is an example of implementing an asynchronous method.
(1) Create a Windows Application Project, add a Button1 on the default Form1. (2) Create a Class Library type Project, change the default Class1 to AsynchronousProcess. (3) Write a long way to consume a time: private sub dressjob (byval parameters as jobargs) 'Simulate the long running job system.threading.thread.sleep (5000) End Sub Note: Jobargs is a custom class with parameters passed to the callee from the caller: Public Class JobArgs Public Duration As Integer 'Milliseconds Public Sub New () Me.Duration = 0 End Sub Public Sub New (ByVal milliSeconds As Integer) Me.Duration = milliSeconds End SubEnd Class
(4) Define a delegate: private delegate () Note: This delegate's signature should be consistent with the time consuming method, for example, if the DOLONGJOB has parameters, then LongrunningOperationDelegate should also have the same parameters.
(5) the preparation of a new public methods for the caller invokes: Public Sub StartLongJob (ByVal milliSeconds As Integer) 'The instance of the delegate Dim myDelegate As New LongRunningOperationDelegate (AddressOf DoLongJob) Dim aResult As System.IAsyncResult aResult = myDelegate.BeginInvoke (New Jobargs (MilliseConds), Nothing, Nothing) End Sub In this new method, the call to DOLONGJOB is converted into calls to the Delegate's BeginInvoke method. In this way, the caller can immediately get the control immediately after calling STARTLONGJOB, to continue to perform subsequent code. (6) Write the calling code of the client, no different ways, such as in the Click event in Button: DIM OBJ AS NEW longoperation.asynchronousProcess Obj.StartLongJob (Cint (TextBox1.text)) This time the program is running, you can find The obj.startlongjob statement is immediately returned, and it is not necessary to wait for a time consuming operation. This is the easiest way of asynchronous methods. Note: The client program needs to add a reference to the Class Library Project.
(7) The called by the caller should also provide a method to perform an event, so that the caller performs some corresponding operations after obtaining the "execution" notice. For this purpose, a method may be modified StartLongJob using System.AsyncCallback class provides custom events: Public Sub StartLongJob (ByVal milliSeconds As Integer) 'The instance of the delegate Dim myDelegate As New LongRunningOperationDelegate (AddressOf DoLongJob)' The instance of AsynchronousCallBack Dim myCallBack As New System.AsyncCallback (AddressOf Me.DoPostJob) Dim aResult As System.IAsyncResult aResult = myDelegate.BeginInvoke (New JobArgs (milliSeconds), myCallBack, Nothing) End Sub wherein, DoPostJob is a function of a triggering event: Public event after JobCompleted (ByVal progressEventArgs as ProgressEventArgs) Private Sub DoPostJob (ByVal ar as System.IAsyncResult) 'Notify the caller of job completion Dim evtArgs as New ProgressEventArgs evtArgs.ProgressPercentage = Delegate completed 100.0 RaiseEvent JobCompleted (evtArgs) End Sub in the called BeginInvoke This function will be called so that it is not necessary to keep polling whether the operation has been completed. Note: ProgressEventArgs is inherited from the event arguments EventArgs class: Public Class ProgressEventArgs Inherits EventArgs Public ProgressPercentage As SingleEnd Class (8) the caller modified accordingly: Dim obj As New LongOperation.AsynchronousProcess AddHandler obj.JobCompleted, AddressOf Job_Completed obj.StartLongJob ( CInt (TextBox1.Text)) wherein, Job_Completed is a program event: Private Sub Job_Completed (ByVal e As LongOperation.ProgressEventArgs) MessageBox.Show (e.ProgressPercentage & "% finished.", Me.Text, MessageBoxButtons.OK) End Sub