在ASP.NET中的Task.Factory定时

时间:2012-04-30 03:38:20

标签: asp.net vb.net timeout taskfactory

我一直在搜索有关异步任务的超时设置的信息,但我找不到任何不涉及wait()的内容。也许我在看这个错误。

我有一个API调用,可能需要10分钟才能完成文件传输。我产生了一个新线程,因此用户不必坐下来观察这种情况。我添加了数据库更新,以便我可以知道发生了什么。上传开始,几分钟后......什么都没有。传输中止,没有异常抛出且没有数据库更新。这必然意味着线程被丢弃了,对吗?如何让它保持更长时间?谢谢你的帮助。

Sub up_load(s As Object, e As EventArgs)

    Dim uploadtask As Task = Task.Factory.StartNew(Sub() begin_upload(), TaskCreationOptions.LongRunning)

    lblmsg.Text = "Successfully initiated upload"
End Sub

Sub begin_upload()

    Dim request As New YouTubeRequest(settings)
    Dim vidupload As New Video()
    Dim cmdupdate As SqlCommand

    vidupload.Title = "My Big Test Movie"
    vidupload.Tags.Add(New MediaCategory("Nonprofit", YouTubeNameTable.CategorySchema))
    vidupload.Keywords = "church, jesus"
    vidupload.Description = "See the entire video"
    vidupload.YouTubeEntry.Private = False
    vidupload.YouTubeEntry.setYouTubeExtension("location", "Downers Grove, IL")
    vidupload.YouTubeEntry.MediaSource = New MediaFileSource("c:\users\greg\test3.asf", "video/x-ms-wmv")

    'updates fine
    cmdupdate = New SqlCommand("Update transactions set name='upload started' where transactionid='18'", conHyperData)
    conHyperData.Open()
    cmdupdate.ExecuteNonQuery()
    conHyperData.Close()

    Try

        'very long running task +5 min
        Dim createdVideo As Video = request.Upload(vidupload)

        'never gets here 
        cmdupdate = New SqlCommand("Update transactions set name='upload finished' where transactionid='18'", conHyperData)
        conHyperData.Open()
        cmdupdate.ExecuteNonQuery()
        conHyperData.Close()
    Catch ex As Exception

        'never thrown, transfer aborts
        cmdupdate = New SqlCommand("Update transactions set name=@name where transactionid='18'", conHyperData)
        cmdupdate.Parameters.AddWithValue("@name", "failed - " & ex.ToString)
        conHyperData.Open()
        cmdupdate.ExecuteNonQuery()
        conHyperData.Close()
    End Try

End Sub

0 个答案:

没有答案