manualresetevent.waitone正在挂起

时间:2014-09-02 06:20:10

标签: windows service timer manualresetevent

我有一个带定时器的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移动了。

(而其他ftpservers将文件留在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

0 个答案:

没有答案