线程中的WebClient事件 [英] WebClient events in threads

查看:75
本文介绍了线程中的WebClient事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多线程应用程序(DownloadFileCompleted和DownloadProgressChanged)中使用WebClient事件时遇到问题.我做了一个检查更新是否可用的函数(CheckUpdates),如果有的话,它叫做DownloadFileAsync.如果我从表单线程调用CheckUpdates函数,那么一切都会正常运行,并且会触发事件(因此我可以正确更新gui).如果我为CheckUpdates创建工作线程(因此gui将在检查期间响应),则不再触发DownloadFileAsync的事件.

代码如下:

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);
...
}



然后我像这样创建线程:
Thread thread = new Thread(CheckUpdates);


有谁知道为什么会这样吗?我做错什么了吗?

提前谢谢.

Matt



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

推荐答案

这里确实没有足够的代码来查看问题所在.但有一些建议.

如果仅更新进度条,请查看 BackgrouWorker .

如果您不希望使用它,则ThreadPool线程提供的线程没有new Thread的开销.

DownloadFileAsync和其他异步调用实际上是自己创建和使用线程的(根据观察,它们似乎是ThreadPool线程).

请记住,您不能从单独的线程更新UI,您必须执行类似
的操作
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;
}



最后,我将检查一下您是否在某种程度上错失了代码,它可能很简单,就像您尝试从错误的线程更新UI一样.



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.


首先谢谢答案,对我的答复延迟感到抱歉.
是的,我知道跨线程问题,我已经在使用MethodInvoker进行调用.
但是我的问题与跨线程异常或任何异常无关.只是当我在与表单线程不同的线程中调用DownloadFileAsync时,我的WebClient对象不会触发关联事件,因此无法更新UI.然后,该过程完成,而没有通知用户发生了什么事情.
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.


这可能是一个愚蠢的问题,但是您要打电话给thread.Start();吗?

尼克
This may be a stupid question, but are you calling thread.Start();?

Nick


这篇关于线程中的WebClient事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆