Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...
Posted
Comments
Al Moje 11-Oct-11 4:42am    
Hi, I think the best way to handle storing and retrieving image from uploaded file
is to store it in the database table as a varbinay(max) type. In such case there is no
much problem on retrieving from table to listview or datagridview and it is safer and feasible....

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.
 
Share this answer
 
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
C#
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:
C#
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).
 
Share this answer
 
v2
Comments
lukeer 11-Oct-11 6:24am    
OT: How can I get rid of the closing string tag?
BobJanova 11-Oct-11 6:59am    
You need to escape <string> in Action<string> as &lt;string&gt;.
lukeer 11-Oct-11 8:09am    
Thanks a lot. (done)

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