使用线程动态显示listviewitem图像 [英] display listviewitem image dynamically using thread

查看:52
本文介绍了使用线程动态显示listviewitem图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我能解决您的问题吗...您看到我想创建一个列表视图,该列表视图的图像列表是从url检索的...所以我的方法是

1.)从数据库加载所有数据并写入xml
2.)从数据库中检索图像的所有URL,并进行Web请求以将给定URL中的实际图像检索到我的图像列表中.
3.)根据应显示的图像将相应的图像分配给listviewitem. (要在listviewitem中显示的图像将从图像列表中检索).

所以我所做的就是在每行中都有图像的URL,我捕获了该值并创建了一个线程,并在该线程中根据所检索的URL进行webrequest.

但是问题是当我尝试显示在线程内检索到的图像时,出现跨线程错误..任何想法和帮助人员将不胜感激...

解决方案

您的标题错误.您不应该尝试通过从URL获取图像的线程来更新UI中的图像.相反,在加载图像时,您应该使列表无效(使其重新绘制),或者应该将控制权转移到UI线程(使用Invoke或BeginInvoke),如Lukeer所示.

您应该考虑将这些图像存储在本地,除非有充分的理由必须将这些图像来自远程URL(例如,如果它们是服务器状态或某些内容的实时更新).除了允许您规避此问题之外,这还意味着您不依赖互联网连接并且外部服务器正常运行(并且未被阻止)才能使您的应用程序正常运行.


我知道WindowsForms中的非法跨线程调用.我想您正在使用它们.

为了克服该错误,您必须避免使用worker thead中任何控件的任何属性.而是让GUI线程执行工作.

  private   void  SetLabelText(  private   void  SetLabelText(字符串文本)
{
    如果(label1.InvokeRequired)
    {
        label1.Invoke(操作< string>(SetLabelText,  object  [] {text}));
        返回;
    }

    label1.Text =文本;
} 

如您所见,立即更改标签的代码保持不变. if -branch使GUI线程再次执行该方法并退出,然后再执行实际工作.在GUI线程中,然后再次执行该方法,而不输入if分支并执行实际的标签更改代码.

由于GUI线程是最初创建label1的线程,因此可以使用其属性(允许任何线程使用特殊属性InvokeRequired以及方法InvokeBeginInvoke).


Hi guys can you please help me out with my problem... you see i want to create a listview that has its imagelists retrieve from the url... so my approach was

1.) load all the data from the database and write into xml
2.) retrieve all the url of the image from the database and make a webrequest to retrieve the actual image from the given url into my imagelist.
3.) assign the corresponding image to the listviewitem depending on the image that it should be displaying. ( the image to be displayed in the listviewitem will be retrieved from the imagelist ).

So what i did was in every row that has an url of the image i captured that value and created a thread and within that thread im doing a webrequest depending on the url retrieved.

but the problem is when i try to display the image retrieved within the thread i get an error of cross-threading error.. any idea and help guys will be much appreciated...

解决方案

Your title is wrong. You shouldn''t be trying to update the image in the UI from the thread which gets it from a URL. Instead, when the image is loaded, you should either invalidate the list (causing it to repaint), or you should transfer control to the UI thread (using Invoke or BeginInvoke), as shown by Lukeer.

You should consider storing these images locally, unless there is a good reason they have to come from a remote URL (for example if they''re live updates of server status or something). As well as allowing you to dodge this issue, it would also mean that you''re not relying on an internet connection and a foreign server being up (and not blocked) for your application to work correctly.


I know illegal cross-thread calls in WindowsForms. I guess that you''re using them.

To overcome the error, you have to refrain from using any property of any control from your worker thead. Instead, make the GUI thread do the work. An example

private void SetLabelText( string text)
{
    label1.Text = text;
}

This one will result in an error when called from a thread other than the GUI thread. Put some thead-border-crossing code in the method:

private void SetLabelText( string text)
{
    if( label1.InvokeRequired)
    {
        label1.Invoke( new Action<string>(SetLabelText, new object[]{text}));
        return;
    }

    label1.Text = text;
}

As you can see, the immediate label-changing code stays the same. The if-branch makes the GUI thread execute the method again and exit before doing the actual work. In the GUI thread, the method is then executed again, not entering the if-branch and executing the actual label changing code.

Since the GUI thread is the one that originally created label1, it is allowed to use its properties (any thread is allowed to use the special property InvokeRequired as well as methods Invoke and BeginInvoke).


这篇关于使用线程动态显示listviewitem图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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