Calling Synchronous Methods Asynchronously – I

In .Net you can call a Synchronous method, asynchronously using delegates. Here is the code

Public Class Form1
Public Delegate Sub JobWithDelayDelegate(ByVal Ar As String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Public MyJobWithDelayDelegate As New JobWithDelayDelegate(AddressOf JobWithDelay)
MyJobWithDelayDelegate.BeginInvoke("Hello", Nothing, Nothing)
End Sub

Private Sub JobWithDelay(ByVal Arg1 As String)
System.Threading.Thread.Sleep(60000)
MessageBox.Show(Arg1)
End Sub
End Class

The JobWithDelay method, if we call synchronously, the UI will hang, and if it is taking too much time there is no option available to cancel the operation too. But using asynchronous call, the UI will not hang, and if you wish you can put a method, which cancel the call too.


MyJobWithDelayDelegate.EndInvoke(Nothing)

You can get more information on this url : Calling Synchronous Methods Asynchronously on MSDN

This entry was posted in .Net. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>