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

前端 未结 4 1838
南方客
南方客 2020-12-30 05:17

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 importa

4条回答
  •  情书的邮戳
    2020-12-30 05:49

    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).

提交回复
热议问题