0

I have code running in background worker, which verifies if mentioned link(website) is valid. if not valid, the procedure gives error and goes on to check another link. To achieve this I m using webrequest and webresponse.

The problem starts when while background worker is accessing links, if I try to download something from main thread(which works totally fine if backgroundworker is not working), the download doesn't complete.

Code for Backgroundworker:

Try
            Dim bgwebRequest As WebRequest
            Dim bgwebresponse As WebResponse

            bgwebRequest = WebRequest.Create(websitelink)
            bgwebRequest.Timeout = 200
            bgwebresponse = bgwebRequest.GetResponse()

            '//code for website found

  Catch ex As Exception

            '//code for website not found

  End Try

code for downloadingfile:

Dim download As WebClient = New WebClient

  download.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; 
  x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 
  Safari/537.36")

  download.DownloadFileAsync(New Uri(directdownloadlink), downloadpathinpc)

  AddHandler download.DownloadFileCompleted, AddressOf OnDownloadComplete1

  download.Dispose()

code for ondownloadcomplete1:

'//run tasks that needs to completed if download is comolete

If backgroundworker is running, the debugger never reaches "ondownloadcomplete1"

  • That does sound rather odd. The `DoWork` event handler and the `DownloadFileCompleted` event handler are both executed on `ThreadPool` threads but I don't see why there'd be an issue executing both. How exactly did you test this? Did you place a breakpoint on `OnDownloadComplete1` and see it get hit in one case and not the other? – jmcilhinney Oct 01 '19 at 04:42
  • A potential workaround is to call `DownloadFileTaskAsync` instead of `DownloadFileAsync`. It is newer - so you'd have to be using a recent enough version of VB - and it uses a different async pattern - so you'd have to change your code structure - but it may avoid the issue you're encountering. I'd be more interested in a solution if you can find one but a workaround may be better anyway, as the newer async pattern is generally superior. I can't guarantee that it wouldn't encounter the same issue though, given that I don;t know what the actual cause is. – jmcilhinney Oct 01 '19 at 04:45
  • yes, I placed a breakpoint on onDownloadcomplete1, the breakpoint never reaches the procedure if backgroundworker is running. – Suraj Singh Oct 01 '19 at 05:34
  • Also,, is is worth mentioning that, mostly this issue is found when backgroundworker's line of code timesout? I dont know if its relevant or not, but just mentioning.. Meanwhile I ll do my research on DownloadFileTaskAsync. Any help would be appreciated. – Suraj Singh Oct 01 '19 at 05:37
  • I changed the code to download.DownloadFileTaskAsync, but of no use.. it downloads a 0 KB file, and doesnt move to OnDownloadComplete1 – Suraj Singh Oct 01 '19 at 06:04
  • You are almost certainly using `DownloadFileTaskAsync` incorrectly. I specifically stated that you would have to change the structure of your code but the fact that you are still expecting to execute and event handler indicates that you ignored that. – jmcilhinney Oct 01 '19 at 07:32
  • I tried looking up for DownloadFileTaskAsync, but dint come across any. Can you help me with that bit for code and explain me how it works? – Suraj Singh Oct 01 '19 at 09:46
  • I tried looking for DownloadFileTaskAsync as i already mentioned. Since I m new to VB.net, the explanation that i got was way too much for me.. I tried looking for Tasks too., but I got code from C#. Since my knowledge for VB.net is from VB and 7 year old. I am unable to understand. – Suraj Singh Oct 03 '19 at 03:54
  • Anyways.. Thanks for your help.. I ll try something that I understand. – Suraj Singh Oct 03 '19 at 03:55
  • Unfortunately, SO is not the place for learning new topics from scratch. It's far more specific than that. There are other places that you can get help on topics like that. – jmcilhinney Oct 03 '19 at 04:20
  • I know its not place for leaning new topics, but I dont think it should be issue if one or two line of code can help someone. – Suraj Singh Oct 03 '19 at 04:54

0 Answers0