如何使用WebClient类的UploadDataAsync方法进行带进度条的FTP循环异步上传

xwchenhui001 2010-04-28 12:06:52
RT,进度条和循环我已经搞好了,现在的问题是,如何让程序等待当前循环运行完毕后再进入下一个循环?

我用了WaitOne方法,根据MSDN上这个实例,不过怎么调试都得不出“File upload is complete.”这个结果,上传开始后程序直接就卡死了。查看FTP发现图片上传已经成功了,不过程序却无法返回结果。


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
...全文
624 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwchenhui001 2010-04-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xingyuebuyu 的回复:]

上传不了是什么意思?
我这里没环境进行测试,不过你要循环上传,最好是在UploadFileCallback方法中开始下一次的上传,用数组存你要上传的地址和文件名,这样就不用循环等待。
[/Quote]
不是,我原本打算换成UploadDataAsync方法上传的,不过搞不定代码,所以上传不了。现在换回来了,有时间再研究那个。

在UploadFileCallback方法中开始下一次的上传,这个思路不错,我试试看,谢谢了
xingyuebuyu 2010-04-29
  • 打赏
  • 举报
回复
上传不了是什么意思?
我这里没环境进行测试,不过你要循环上传,最好是在UploadFileCallback方法中开始下一次的上传,用数组存你要上传的地址和文件名,这样就不用循环等待。
xwchenhui001 2010-04-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xingyuebuyu 的回复:]

VB.NET code
While bolFinish = False
''加入sleep可以降低CPU使用率
System.Threading.Thread.Sleep(20)

Application.DoEvents()
End While




你直接调用UploadFileInBackground方法会阻止不?

仍……
[/Quote]
是我的代码写错了,我单独测试UploadDataAsync的时候发现根本上传不了。不知道还有更好的办法没?这样循环调用Application.DoEvents()有点治标不治本的味道,呵呵
xingyuebuyu 2010-04-29
  • 打赏
  • 举报
回复
While bolFinish = False
''加入sleep可以降低CPU使用率
System.Threading.Thread.Sleep(20)

Application.DoEvents()
End While



你直接调用UploadFileInBackground方法会阻止不?

仍然分别加入之前的代码看UploadFileInBackground和UploadFileCallback是否是在同一个线程中运行?
Console.WriteLine("Upload Complete thread id " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString)
xwchenhui001 2010-04-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xingyuebuyu 的回复:]
VB.NET code

Public Shared Sub UploadFileInBackground(ByVal address As String, ByVal fileName As String)

Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(Fa……
[/Quote]
谢谢这位大哥,你的方法确实可行,不过为什么我改用UploadDataAsync的方法就不行了呢?程序执行到最后依然卡住,CPU占用100%

    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


另外,我是在另一个过程中call上传过程的
xingyuebuyu 2010-04-28
  • 打赏
  • 举报
回复

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


你先不使用System.Threading.AutoResetEvent,看下程序运行结果就会发现Upload thread id和
"Upload Complete thread id "是一样的,说明异步上传过程虽然使用的是线程池中的线程,但是上传完成后要回调的函数还是你之前开始异步上传的线程,但是因为你在开始异步上传后使用waiter.WaitOne()
阻止了当前线程,所以线程池中的线程回调你的上传完成函数也被阻止了,无法进行调用.


 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
xwchenhui001 2010-04-28
  • 打赏
  • 举报
回复
汗,没用的。
WaitOne下面的语句是等Set上面的语句运行完后才运行的,跟假死无关。
照理说,图片传完,Set上面的语句应该也运行了,不知道怎么搞的,就是看不到输出结果,程序直接死了。
yuanhuiqiao 2010-04-28
  • 打赏
  • 举报
回复
加个Application.Doevent()试试

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧