Synchronous code executes in the order in which it occurs in the compiled program. Asynchronous code executes out of order. When an asynchronous call occurs, the call is made to the asynchronous invoke method and immediately returns. The flow of execution resumes while the asynchronous process finishes separately. You can write code to block until the asynchronous process finishes, or you can pass a callback method to the asynchronous process so it will notify you by invoking the callback when the data is ready. The ## Programming Synchronous Behavior Listing 6.2 demonstrates a synchronous call to the ## Listing 6.2 A Synchronous Call to a Recursive Method1: Private Delegate Function MyDelegate(ByVal Numerator As Long, _ 2: ByVal Denominator As Long) As Long 3: 4: Private Function Calculate(ByVal Numerator As Long, _ 5: ByVal Denominator As Long) As Long 6: 7: If (Numerator Mod Denominator = 0) Then 8: Return Denominator 9: Else 10: Return Calculate(Denominator, Numerator Mod Denominator) 11: End If 12: 13: End Function 14: 15: Private Sub MenuItem5_Click(ByVal sender As System.Object, _ 16: ByVal e As System.EventArgs) Handles MenuItem5.Click 17: 18: SetResult(Numerator, Denominator, _ 19: Invoke(New MyDelegate(AddressOf Calculate), _ 20: New Object() {Numerator, Denominator})) 21: 22: End Sub 23: 24: Private Sub SetResult(ByVal Numerator As Long, _ 25: ByVal Denominator As Long, ByVal Gcd As Long) 26: 27: Const mask As String = _ 28: "{0}. The greatest common divisor of {1} and {2} is {3}" 29: 30: Static I As Integer 31: I += 1 32: 33: ListBox1.Items.Insert(0, _ 34: String.Format(mask, I, Numerator, Denominator, Gcd)) 35: End Sub When we call the Technically you do not have to call ## Programming Asynchronous Behavior The asynchronous method ## Listing 6.3 An Asynchronous Method Invocation1: Private Sub SetResult(ByVal Numerator As Long, _ 2: ByVal Denominator As Long, ByVal Gcd As Long) 3: 4: Const mask As String = _ 5: "{0}. The greatest common divisor of {1} and {2} is {3}" 6: 7: Static I As Integer 8: I += 1 9: 10: ListBox1.Items.Insert(0, _ 11: String.Format(mask, I, Numerator, Denominator, Gcd)) 12: End Sub 13: 14: Private Delegate Sub AsynchDelegate(ByVal Numerator As Long, _ 15: ByVal Denominator As Long) 16: 17: Private Sub AsynchCalculate(ByVal Numerator As Long, _ 18: ByVal Denominator As Long) 19: 20: SetResult(Numerator, Denominator, _ 21: Calculate(Numerator, Denominator)) 22: 23: End Sub 24: 25: Private Sub MenuItem6_Click(ByVal sender As System.Object, _ 26: ByVal e As System.EventArgs) Handles MenuItem6.Click 27: 28: BeginInvoke(New AsynchDelegate(AddressOf AsynchCalculate), _ 29: New Object() {Numerator, Denominator}) 30: 31: End Sub The biggest difference between Listings 6.2 and 6.3 is that we call
## Completing Asynchronous Calls Our example in Listing 6.3 calls a subroutine, There are four ways to complete an asynchronous call. You can poll the The ## Polling |

Visual Basic(R) .NET Power Coding

ISBN: 0672324075

EAN: 2147483647

EAN: 2147483647

Year: 2005

Pages: 215

Pages: 215

Authors: Paul Kimmel

flylib.com Â© 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net