0

我有一个带有计时器的 Windows 服务。定时器每天大约 3 次将文件上传到不同的 ftp 服务器。我设置计时器,上传文件,然后设置下一次。这工作了一段时间,直到我添加了另一个用于上传文件的 ftpserver。上传到该 ftpserver 时,项目在 manualresetevent.waitone 挂起(即使文件夹已上传) 这里是部分代码,如果需要更多,请告诉我。

Dim state As New FtpState
                    Dim request As FtpWebRequest = DirectCast(WebRequest.Create(target), FtpWebRequest)
                    request.Method = WebRequestMethods.Ftp.UploadFile

                    request.Credentials = mycredentials 

                    state.Request = request
                    state.FileName = fileName

                    ' Get the event to wait on.
                    waitObject = state.OperationComplete

                    ' Asynchronously get the stream for the file contents.
                    request.BeginGetRequestStream(New AsyncCallback(AddressOf EndGetStreamCallback), state)

                    ' Block the current thread until all operations are complete.
                    waitObject.WaitOne()

                    ' The operations either completed or threw an exception. 
                    If state.OperationException IsNot Nothing Then
                        Throw New Exception(state.OperationException.ToString)
                    Else

                        Publish.sendMail("Upload completed for filename:" & fileName & state.StatusDescription)
                    End If
                End If

这个 ftpserver 的工作方式与我正在使用的其他服务器略有不同,我不确定这是否是问题的原因。

不同之处在于:我上传了一个可能非常大的 zip 文件夹(不仅仅是文件),并且在上传后不久,它就会从该 ftpserver 中移出。

(而其他 ftpserver 将文件留在 ftpserver 上)

我认为这个问题只有在 zipfolder 变大后才开始。

我知道它已上传,然后从那里删除。

那么如果上传完成了,为什么会卡在waitone呢?

这是我的 endstreamcallback 函数

Private Shared Sub EndGetStreamCallback(ByVal ar As IAsyncResult)
    Dim state As ftpState = DirectCast(ar.AsyncState, ftpState)

    Dim requestStream As Stream = Nothing
    ' End the asynchronous call to get the request stream. 
    Try
        requestStream = state.Request.EndGetRequestStream(ar)
        ' Copy the file contents to the request stream. 
        Const bufferLength As Integer = 2048
        Dim buffer As Byte() = New Byte(bufferLength - 1) {}
        Dim count As Integer = 0
        Dim readBytes As Integer = 0
        Dim stream As FileStream = File.OpenRead(state.FileName)
        Do
            readBytes = stream.Read(buffer, 0, bufferLength)
            requestStream.Write(buffer, 0, readBytes)
            count += readBytes
        Loop While readBytes <> 0
        'Console.WriteLine("Writing {0} bytes to the stream.", count)
        ' IMPORTANT: Close the request stream before sending the request.
        requestStream.Close()
        ' Asynchronously get the response to the upload request.
        state.Request.BeginGetResponse(New AsyncCallback(AddressOf EndGetResponseCallback), state)
        ' Return exceptions to the main application thread. 
    Catch e As Exception
        Publish.sendMail("Could not get the request stream.")
        state.OperationException = e
        state.OperationComplete.[Set]()
        Return
    End Try

End Sub

' EndGetResponseCallback 方法
' 完成对 BeginGetResponse 的调用。

   Private Shared Sub EndGetResponseCallback(ByVal ar As IAsyncResult)
    Dim state As FtpState = DirectCast(ar.AsyncState, FtpState)
    Dim response As FtpWebResponse = Nothing
    Try
        response = DirectCast(state.Request.EndGetResponse(ar), FtpWebResponse)
        response.Close()
        state.StatusDescription = response.StatusDescription
        ' Signal the main application thread that  
        ' the operation is complete.
        state.OperationComplete.[Set]()
        ' Return exceptions to the main application thread. 
    Catch e As Exception
        Publish.sendMail("Error getting response.")
        state.OperationException = e
        state.OperationComplete.[Set]()
    End Try
End Sub
4

0 回答 0