使用Flickr.Net API时,即使在使用Backgroundworker之后,窗体UI也会冻结 [英] Form UI Freeezes even after using Backgroundworker when using Flickr.Net API

查看:107
本文介绍了使用Flickr.Net API时,即使在使用Backgroundworker之后,窗体UI也会冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Flickr.net API上传一些图像。图像已上传但用户界面冻结。我在后台工作人员中插入了要上传的代码

Im trying to upload some images using the Flickr.net API.The Images are uploaded but the User Interface freezes.I have inserted the code for uploading in a Background worker

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    foreach (var item in imagelist)
    {
        flickr.UploadPicture(item, Path.GetFileName(item), null, null, true, false, true);
    }
    MessageBox.Show("Success");
}

flickr对象是由另一种形式创建的,并传递给该形式。当单击按钮时,我用 if(worker.IsBusy == false){backgroundWorker1.RunWorkerAsync();} 来调用该工人。

The flickr object is created earlier from another form and passed to this form. I call the worker with if(worker.IsBusy==false){backgroundWorker1.RunWorkerAsync();} when a button is clicked.

推荐答案

导致此问题的两个常见原因,您的代码段太简短了,无法缩小可能的范围。首先是ReportProgress方法,事件处理程序在UI线程上运行。如果您经常调用它,则UI线程可能会被调用请求淹没,并花费太多时间来处理它们。它不再能够履行其日常职责,例如响应绘画请求和处理用户输入。因为一旦处理完一个调用请求,就会有另一个等待分派。 UI线程实际上并没有冻结,而是看起来像是冻结了。最终效果是相同的。您需要通过降低工作进程的速度来解决此问题,或者减少调用ReportProgress的频率。

Two common causes for this, your snippet is way too brief to narrow down which it might be. First is the ReportProgress method, the event handler runs on the UI thread. If you call it too often then the UI thread can get flooded with invoke requests and spend too much time to handle them. It doesn't get around to doing its regular duties anymore, like responding to paint requests and processing user input. Because as soon as it is done handling a invoke request, there's another one waiting to get dispatched. The UI thread isn't actually frozen, it just looks like it is. The net effect is the same. You'll need to fix it by slowing down the worker or call ReportProgress less often.

第二个原因是您的 flicker 对象没有线程-safe,并确保以线程安全的方式使用它。通过自动整理从工作线程到UI线程的调用。这对于COM组件非常常见,这种封送处理是COM的核心功能。同样,UI线程实际上并没有冻结,但是由于它正在忙于上传照片,因此它仍然无法处理绘画和输入。您需要通过在辅助线程上创建 flicker 对象来对其进行修复。很有可能您无法使用BackgroundWorker做到这一点,所以这样的组件通常需要STA线程来发送消息循环。这需要Thread.SetApartmentState()和Application.Run()。

The second cause is your flicker object not being thread-safe and itself ensuring that it is used in a thread-safe way. By marshaling the call from the worker thread to the UI thread automatically. This is very common for COM components, this kind of marshaling is a core feature of COM. Again the UI thread isn't actually frozen, but it still won't handle paint and input since it is busy uploading a photo. You'll need to fix it by creating the flicker object on the worker thread. With good odds that you can't do this with a BackgroundWorker, such a component often needs an STA thread that pumps a message loop. Which requires Thread.SetApartmentState() and Application.Run().

这篇关于使用Flickr.Net API时,即使在使用Backgroundworker之后,窗体UI也会冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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