如何在 vb.net 中进行非常简单的异步方法调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10398490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a very simple asynchronous method call in vb.net
提问by RichC
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).
我只有一个简单的 vb.net 网站,它需要调用一个 Sub 来执行一个很长的任务,该任务与同步文件系统中的某些目录有关(细节不重要)。
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.
我只想防止超时,所以我只想异步调用 Sub 。我不需要(甚至不需要)和回调/确认它成功运行。
So, how can I call my method asynchronously inside a website using VB.net?
那么,如何使用 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
回答by Timiz0r
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 thisfor a good read.
Thread
除非您需要对线程进行更多控制,否则我不建议使用该类,因为创建和拆除线程的成本很高。相反,我建议使用ThreadPool thread。看到这个很好读。
You can execute your method on a ThreadPool
thread like this:
您可以在这样的ThreadPool
线程上执行您的方法:
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).
最后,还要注意其他线程中未处理的异常会杀死 IIS。请参阅这篇文章(旧但仍然相关;但不确定解决方案,因为我并不真正使用 ASP.NET)。
回答by J...
You could do this with a simple thread:
你可以用一个简单的线程来做到这一点:
Add :
添加 :
Imports System.Threading
And wherever you want it to run :
无论您希望它在哪里运行:
Dim t As New Thread(New ThreadStart(AddressOf DoAsyncWork))
t.Priority = Threading.ThreadPriority.Normal
t.Start()
The call to t.Start()
returns immediately and the new thread runs DoAsyncWork
in the background until it completes. You would have to make sure that everything in that call was thread-safe but at first glance it generally seems to be so already.
调用t.Start()
立即返回,新线程DoAsyncWork
在后台运行,直到完成。您必须确保该调用中的所有内容都是线程安全的,但乍一看通常似乎已经如此。
回答by spinjector
I also was looking for information on Asynchronous programming in VB. In addition to this thread, I also found the following: beginning with Visual Studio 2012 and .Net Framework 4.5, VB was given two new keywords to make a method asynchronous right in the declaration, without using Thread or Threadpool. The new keywords are "Async" and "Await". You may refer to the following links if you wish:
我也在寻找有关 VB 异步编程的信息。除了这个线程之外,我还发现了以下内容:从 Visual Studio 2012 和 .Net Framework 4.5 开始,VB 被赋予了两个新关键字,以便在声明中使方法异步,无需使用 Thread 或 Threadpool。新的关键字是“Async”和“Await”。如果您愿意,可以参考以下链接:
http://msdn.microsoft.com/library/hh191443%28vs.110%29.aspx
http://msdn.microsoft.com/library/hh191443%28vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/hh191564%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/hh191564%28v=vs.110%29.aspx
回答by Critical Error
This is an older thread, but I figured I'd add to it anyway as I recently needed to address this. If you want to use the ThreadPool to call a method with parameters, you can modify @Timiz0r's example as such:
这是一个较旧的线程,但我想无论如何我都会添加它,因为我最近需要解决这个问题。如果要使用 ThreadPool 调用带参数的方法,可以将@Timiz0r 的示例修改为:
System.Threading.ThreadPool.QueueUserWorkItem(Sub() MethodName( param1, param2, ...))