Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm having a problem using the WebClient events in a multi threaded application (DownloadFileCompleted and DownloadProgressChanged). I made a function that checks if updates are available (CheckUpdates),and if there's any, it calls a DownloadFileAsync. Everything works fine and events are fired (so I can update the gui correctly) if I call that CheckUpdates function from the form thread. If I make a worker thread for CheckUpdates (so the gui will be responsive during the check), DownloadFileAsync's events are not fired anymore.

Here's the code:

private void CheckUpdates()
{
...
DownloadUpdate(Uri uri);
...
}


private void DownloadUpdate(Uri uri)
{
...
webClient.DownloadProgressChanged += (o, t) => {  progressBar.Value = t.ProgressPercentage; };
webClient.DownloadFileAsync(uri,filename);
...
}


And i create the thread simply like this:
Thread thread = new Thread(CheckUpdates);


Does anyone have an idea about why this happens? Am I doing something wrong?

Thanks in advance.

Matt
Posted

There really isn't enough code here to see what the problem might be. But a few suggestions.

If you are only updating a progess bar, look at the BackgrouWorker.

If you don't wish to use that, ThreadPool threads provide threads that don't have the overhead of new Thread.

DownloadFileAsync and other async calls actually create and use threads themselves (based on observation they appear to be ThreadPool threads).

Remember you CAN'T update the UI from a separate thread, You MUST do something like

private void UpdateUI(int progress)
{
   //frmMain could be "this"
   if(frmMain.InvokeRequired)
   {
        Action d = UpdateUI;
        this.Invoke(d, new object[]{progress};
        return;
   }   
   //if the invoke wasn't required.
   progressBar1.Value = progress;
}


Finally I would check and see if you are somehow erring out on your code, it may be as simple as you are trying to update the UI from the wrong thread.
 
Share this answer
 
v2
First of all thank you for the answer and sorry for my reply delay.
Yes I know about cross threads problems, I was already using invoking with the MethodInvoker.
But my problem is not about a cross-thread exception or any exception at all. Simply when I call the DownloadFileAsync in a thread different from the form thread, my WebClient object does not fire associated events, so I cant update the UI. The process then completes without informing the user about whats going on.
 
Share this answer
 
This may be a stupid question, but are you calling thread.Start();?

Nick
 
Share this answer
 
Yes I was starting the thread :)

Nevermind, seems like I managed to make it work.
 
Share this answer
 
Comments
Member 4688366 24-Aug-12 10:36am    
I have exactly the same problem. Can anybody offer a solution? Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900