16,554
社区成员
发帖
与我相关
我的任务
分享
Public Shared Sub UploadFileInBackground(ByVal address As String, ByVal fileName As String)
Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
Dim client As WebClient = New WebClient()
Dim method As String = "STOR"
Dim uri as Uri = New Uri(address)
' Specify that that UploadFileCallback method gets called
' when the file upload completes.
AddHandler client.UploadFileCompleted, AddressOf UploadFileCallback
client.UploadFileAsync(uri, method, fileName, waiter)
' Block the main application thread. Real applications
' can perform other tasks while waiting for the upload to complete.
waiter.WaitOne()
Console.WriteLine("File upload is complete.")
End Sub
Public Shared Sub UploadFileCallback(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs)
Dim waiter As System.Threading.AutoResetEvent = CType(e.UserState, System.Threading.AutoResetEvent)
Try
Dim reply As String = System.Text.Encoding.UTF8.GetString(e.Result)
Console.WriteLine(reply)
Finally
' If this thread throws an exception, make sure that
' you let the main application thread resume.
waiter.Set()
End Try
End Sub
While bolFinish = False
''加入sleep可以降低CPU使用率
System.Threading.Thread.Sleep(20)
Application.DoEvents()
End While
Public Shared bolFinish As Boolean = False
Public Shared Sub UploadFileInBackground(ByVal address As String, ByVal fileName As String)
Dim client As WebClient = New WebClient()
Dim method As String = "STOR"
Dim uri As Uri = New Uri(address)
Dim myStream As FileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
Dim data() As Byte = New Byte(myStream.Length) {}
myStream.Read(Data, 0, myStream.Length)
bolFinish = False
AddHandler client.UploadProgressChanged, New UploadProgressChangedEventHandler(AddressOf Form1.wc_UploadProgressChanged)
AddHandler client.UploadFileCompleted, AddressOf UploadFileCallback
client.UploadDataAsync(uri, method, Data)
'client.UploadFileAsync(uri, method, fileName)
While bolFinish = False
Application.DoEvents()
End While
Console.WriteLine("File upload is complete.")
End Sub
Public Shared Sub UploadFileCallback(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs)
bolFinish = True
End Sub
Public Shared Sub UploadFileInBackground(ByVal address As String, ByVal fileName As String)
Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
Dim client As WebClient = New WebClient()
Dim method As String = "STOR"
Dim uri as Uri = New Uri(address)
' Specify that that UploadFileCallback method gets called
' when the file upload completes.
AddHandler client.UploadFileCompleted, AddressOf UploadFileCallback
client.UploadFileAsync(uri, method, fileName, waiter)
' Block the main application thread. Real applications
' can perform other tasks while waiting for the upload to complete.
''waiter.WaitOne()
Console.WriteLine("Upload thread id "+System.Threading.Thread.CurrentThread.ManagedThreadId.ToString)
Console.WriteLine("File upload is complete.")
End Sub
Public Shared Sub UploadFileCallback(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs)
Dim waiter As System.Threading.AutoResetEvent = CType(e.UserState, System.Threading.AutoResetEvent)
Try
Dim reply As String = System.Text.Encoding.UTF8.GetString(e.Result)
Console.WriteLine(reply)
Finally
' If this thread throws an exception, make sure that
' you let the main application thread resume.
Console.WriteLine("Upload Complete thread id " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString)
''waiter.Set()
End Try
End Sub
Public Shared bolFinish As Boolean = False
Public Shared Sub UploadFileInBackground(ByVal address As String, ByVal fileName As String)
'Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
Dim client As WebClient = New WebClient()
Dim method As String = "STOR"
Dim uri As Uri = New Uri(address)
bolFinish = False
' Specify that that UploadFileCallback method gets called
' when the file upload completes.
AddHandler client.UploadFileCompleted, AddressOf UploadFileCallback
client.UploadFileAsync(uri, method, fileName)
While bolFinish = False
Application.DoEvents()
End While
' Block the main application thread. Real applications
' can perform other tasks while waiting for the upload to complete.
'waiter.WaitOne()
Console.WriteLine("File upload is complete.")
End Sub
Public Shared Sub UploadFileCallback(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs)
' Dim waiter As System.Threading.AutoResetEvent = CType(e.UserState, System.Threading.AutoResetEvent)
Try
Dim reply As String = System.Text.Encoding.UTF8.GetString(e.Result)
Console.WriteLine(reply)
Finally
' If this thread throws an exception, make sure that
' you let the main application thread resume.
' waiter.Set()
bolFinish = True
End Try
End Sub