Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a very simple asynchronous method call in vb.net

I just have a simple vb.net website that need to call a Sub that performs a very long task that works with syncing up some directories in the filesystem (details not important).

When I call the method, it eventually times out on the website waiting for the sub routine to complete. However, even though the website times out, the routine eventually completes it's task and all the directories end up as they should.

I want to just prevent the timeout so I'd like to just call the Sub asynchronously. I do not need (or even want) and callback/confirmation that it ran successfully.

So, how can I call my method asynchronously inside a website using VB.net?

If you need to some code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Call DoAsyncWork()
End Sub

Protected Sub DoAsyncWork()
        Dim ID As String = ParentAccountID
        Dim ParentDirectory As String = ConfigurationManager.AppSettings("AcctDataDirectory")
        Dim account As New Account()
        Dim accts As IEnumerable(Of Account) = account.GetAccounts(ID)

        For Each f As String In My.Computer.FileSystem.GetFiles(ParentDirectory)
            If f.EndsWith(".txt") Then
                Dim LastSlashIndex As Integer = f.LastIndexOf("\")
                Dim newFilePath As String = f.Insert(LastSlashIndex, "\Templates")
                My.Computer.FileSystem.CopyFile(f, newFilePath)
            End If
        Next

        For Each acct As Account In accts
            If acct.ID <> ID Then
                Dim ChildDirectory As String = ConfigurationManager.AppSettings("AcctDataDirectory") & acct.ID
                If My.Computer.FileSystem.DirectoryExists(ChildDirectory) = False Then
                    IO.Directory.CreateDirectory(ChildDirectory)
                End If
                My.Computer.FileSystem.DeleteDirectory(ChildDirectory, FileIO.DeleteDirectoryOption.DeleteAllContents)
                My.Computer.FileSystem.CopyDirectory(ParentDirectory, ChildDirectory, True)
            Else
            End If
        Next
End Sub
like image 774
RichC Avatar asked May 01 '12 13:05

RichC


People also ask

How do you call a method asynchronously?

The simplest way to execute a method asynchronously is to start executing the method by calling the delegate's BeginInvoke method, do some work on the main thread, and then call the delegate's EndInvoke method. EndInvoke might block the calling thread because it does not return until the asynchronous call completes.

What is asynchronous method in VB net?

Return Types An async method is either a Sub procedure, or a Function procedure that has a return type of Task or Task<TResult>. The method cannot declare any ByRef parameters. You specify Task(Of TResult) for the return type of an async method if the Return statement of the method has an operand of type TResult.

What is an asynchronous call?

An asynchronous method call is a method used in . NET programming that returns to the caller immediately before the completion of its processing and without blocking the calling thread.

How do you call async method in page load?

How can I call a async method on Page_Load ? If you change the method to static async Task instead of void, you can call it by using SendTweetWithSinglePicture("test", "path"). Wait() . Avoid async void unless you are using it for events.


1 Answers

I wouldn't recommend using the Thread class unless you need a lot more control over the thread, as creating and tearing down threads is expensive. Instead, I would recommend using a ThreadPool thread. See this for a good read.

You can execute your method on a ThreadPool thread like this:

System.Threading.ThreadPool.QueueUserWorkItem(AddressOf DoAsyncWork)

You'll also need to change your method signature to...

Protected Sub DoAsyncWork(state As Object) 'even if you don't use the state object

Finally, also be aware that unhandled exceptions in other threads will kill IIS. See this article (old but still relevant; not sure about the solutions though since I don't reaslly use ASP.NET).

like image 75
Timiz0r Avatar answered Sep 23 '22 09:09

Timiz0r