如何使vb.net一个非常简单的异步方法调用 [英] How to make a very simple asynchronous method call in vb.net

查看:281
本文介绍了如何使vb.net一个非常简单的异步方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有一个简单的vb.net网站,需要调用执行一个非常艰巨的任务,随着文件系统(细节并不重要)同步了一些目录,一个工作小组。

当我调用该方法,它最终超时网站等待子例程来完成上。不过,尽管该网站超时,最终常规完成它的任务,所有的目录,因为他们应该结束了。

我只想prevent超时,所以我想只要调用子异步。我不需要(甚至想)和回调/确认它已成功运行。

所以,我怎么能叫我的方法使用异步一个VB.net里面的网站?

如果你需要一些code:

 保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
    呼叫DoAsyncWork()
结束小组保护小组DoAsyncWork()
        昏暗的ID作为字符串= ParentAccountID
        昏暗ParentDirectory作为字符串= ConfigurationManager.AppSettings(AcctDataDirectory)
        昏暗的帐户作为新帐户()
        昏暗accts为IEnumerable(账户)= account.GetAccounts(ID)        对于每个F作为字符串在My.Computer.FileSystem.GetFiles(ParentDirectory)
            如果f.EndsWith(TXT),然后
                昏暗LastSlashIndex作为整数= f.LastIndexOf(\\)
                昏暗newFilePath作为字符串= f.Insert(LastSlashIndex,\\模板)
                My.Computer.FileSystem.CopyFile(F,newFilePath)
            万一
        下一个        对于每一个ACCT方式帐户在accts
            如果acct.ID<> ID然后
                昏暗ChildDirectory作为字符串= ConfigurationManager.AppSettings(AcctDataDirectory)及acct.ID
                如果My.Computer.FileSystem.DirectoryExists(ChildDirectory)= false,那么
                    IO.Directory.CreateDirectory(ChildDirectory)
                万一
                My.Computer.FileSystem.DeleteDirectory(ChildDirectory,FileIO.DeleteDirectoryOption.DeleteAllContents)
                My.Computer.FileSystem.CopyDirectory(ParentDirectory,ChildDirectory,真)
            其他
            万一
        下一个
结束小组


解决方案

我不推荐使用类,除非你需要对线程有更多的控制,如创建和拆除线程是昂贵的。相反,我会建议使用一个线程池线程。 <一href=\"http://blogs.msdn.com/b/pedram/archive/2007/08/05/dedicated-thread-or-a-threadpool-thread.aspx\">See这获取一个良好的阅读。

您可以在这样一个线程池线程中执行你的方法

  System.Threading.ThreadPool.QueueUserWorkItem(AddressOf DoAsyncWork)

您还需要你的方法签名更改为...

 保护小组DoAsyncWork(状态为对象),即使你不使用的状态对象

最后,也知道,在其他线程未处理的异常会杀了IIS。请参见这篇文章(老但仍相关;不能确定,虽然因为我不reaslly使用ASP.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

解决方案

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

这篇关于如何使vb.net一个非常简单的异步方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆