Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用线程上传文件_Vb.net_Multithreading - Fatal编程技术网

Vb.net 使用线程上传文件

Vb.net 使用线程上传文件,vb.net,multithreading,Vb.net,Multithreading,我已经编写了一个使用线程上传文件的代码。我在页面上放置了一个简单的文件上传控件和一个按钮。代码如下所示- Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpload.Click Dim timeStart As TimeSpan = Nothing Dim timeEnd As TimeSpa

我已经编写了一个使用线程上传文件的代码。我在页面上放置了一个简单的文件上传控件和一个按钮。代码如下所示-

Protected Sub btnUpload_Click(ByVal sender As Object,
                               ByVal e As EventArgs) Handles btnUpload.Click
    Dim timeStart As TimeSpan = Nothing
    Dim timeEnd As TimeSpan = Nothing
    Dim timeDiff As TimeSpan = Nothing
    Dim ex As Exception = Nothing
    Dim FileNameWithoutExtension As String = String.Empty

    Try
        Dim objTh As Thread = Nothing
        objTh = New Thread(AddressOf SaveFileByBuffering)
        timeStart = DateTime.Now.TimeOfDay
        objTh.IsBackground = True


        FileNameWithoutExtension = System.IO.Path.GetFileName(FldUploadThreading.FileName)
        objTh.Start("New_" + FileNameWithoutExtension)
        objTh.Name = "ARAThreadFileBuffer"
        objTh.Join()

        timeEnd = DateTime.Now.TimeOfDay
        timeDiff = timeEnd - timeStart

    Catch exThAbort As ThreadAbortException
        ex = exThAbort
    Catch exTh As ThreadStartException
        ex = exTh
    Catch exCommon As Exception
        ex = exCommon
    End Try

End Sub
要使用线程调用的方法:

Public Function SaveFileByBuffering(ByVal lstrFilePath As String)
    Dim bufferSize As Integer = 512
    Dim buffer As Byte() = New Byte(bufferSize - 1) {}


    Dim pathUrl As String = ConfigurationManager.AppSettings("strFilePath").ToString()

    Dim uploadObj As UploadDetail = New UploadDetail()
    uploadObj.IsReady = True
    uploadObj.FileName = lstrFilePath
    uploadObj.ContentLength = Me.FldUploadThreading.PostedFile.ContentLength

    Me.Session("UploadXDetail") = uploadObj

    Dim Upload As UploadDetail = DirectCast(Me.Session("UploadXDetail"), UploadDetail)

    Dim fileName As String = Path.GetFileName(Me.FldUploadThreading.PostedFile.FileName)

    Using fs As New FileStream(Path.Combine(pathUrl, lstrFilePath), FileMode.Create)
        While Upload.UploadedLength < Upload.ContentLength
            Dim bytes As Integer = Me.FldUploadThreading.PostedFile.InputStream.Read(buffer, 0, bufferSize)
            fs.Write(buffer, 0, bytes)
            Upload.UploadedLength += bytes
        End While
    End Using
End Function
公共函数SaveFileByBuffering(ByVal lstrFilePath作为字符串)
Dim bufferSize为整数=512
Dim buffer As Byte()=新字节(bufferSize-1){}
Dim pathUrl为String=ConfigurationManager.AppSettings(“strFilePath”).ToString()
Dim uploadObj As UploadDetail=新UploadDetail()
uploadObj.IsReady=True
uploadObj.FileName=lstrFilePath
uploadObj.ContentLength=Me.FldUploadThreading.PostedFile.ContentLength
Me.Session(“UploadXDetail”)=uploadObj
Dim Upload As UploadDetail=DirectCast(Me.Session(“UploadXDetail”),UploadDetail)
Dim文件名为String=Path.GetFileName(Me.FldUploadThreading.PostedFile.fileName)
将fs用作新的文件流(Path.Combine(pathUrl,lstrFilePath),FileMode.Create)
而Upload.UploadedLength
有两个问题:

  • 当有人同时单击同一个按钮时,线程行为以不同的方式工作,有时页面会崩溃

  • 当我在60个用户的多用户环境中测试了这个过程,每个用户的文件大小为25MB时,页面崩溃了


  • 我有.net 3.5,因此我没有选择2010年或更高版本的文件上载高级版本。

    这不是c#,请使用正确的标记,不要将它们放在页面崩溃时给出的标题错误中?这也是ASP.net?打开
    选项Strict
    选项Explicit
    。您应该看到行
    objTh=New Thread(AddressOf SaveFileByBuffering)
    无效,因为该方法需要一个参数-