Stopping a WebClient.DownloadData

  • Thread starter Thread starter Crirus
  • Start date Start date
C

Crirus

Hi there!

Fisrt the question: there is a ay to make the DownloadData when I want?

I have a WebClient class
I use myWebClient.DownloadData(remoteUrl) to connect to internet server
All this in a separate thread for asincron pourpose
Anyway, when I try to close the application before DownloadData returned, it
wont stop this thread.

The thread run untill a bool variable is set..but if the thread is not on
Reset state I cant send the Set call, or, even if I call it, the Reset is
next

this is the running function for the thread:

Private Sub start()
Do While True
paused.WaitOne() 'wait on the pause event...
If isCancelled = True Then Exit Do
Try
Dim datas, sData As Byte()
Dim strData As String
'sData = getBytes()
SetHeaders()
datas = wbclient.DownloadData(SERVER_ADDRESS)
'datas = wbclient.UploadData(SERVER_ADDRESS, sData) ' signal
back to the caller that we're done...
strData = Encoding.ASCII.GetString(datas)
Dim responseHeaders As New WebHeaderCollection
responseHeaders = wbclient.Headers()

onResponse.Invoke(datas)
paused.Reset() ' pause the thread...
Catch ex As WebException
onResponseError.Invoke(ex.Message)
paused.Reset() ' pause the thread...
End Try
Loop
End Sub
 
Hi Crirus,
I did not look very in detail to your code, but I have the download in a
seperate thread and "abort" that thread in the situation, I thought, you did
describe and I thought it works (I never know with the webclient)..
Cor
 
I use a reset/set system to sleep the thread and a while loop.
As long as Cancel is false, the thread remain in while and sleep or run...
If I make Cancel=false then the while exit and the thread finish.

So, the ideea is that when the thread running routine wait for data in that
method, if I try to cancel the thread he is allredy running so no Cancel is
tested
 
Hi Crirus,
That I did test to, all other methods, keep the thread alive, only the
abort did give me a solution. (It kills it in a way, but a thread is never
killed really direct by your program as far as I know, but it stops
downloading, I told I am not sure, but that is only a very little bit).
Cor
 
Solved...when DownloadData return I just kill the thread if Cancel is
true...usualy that delay hapend when webClient cant find the host
 
Back
Top