Click here to Skip to main content
15,906,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI...
I'm doing a winform application ,and i want to modifed an listview from thread ...
I have used this instruction but it is wrong :
Action action =>(string) listView1.Items[0].SubItems[0].Text = (fff++).ToString();
this.Invoke(action);
Posted

Have a look at this thread here.
 
Share this answer
 
Comments
guendouz bachir 19-Apr-11 12:13pm    
yes thx you but :
this thread make call to a method and this method make call to an method and this final modifed the listview what i can write the final code to change the listview?
Sergey Alexandrovich Kryukov 19-Apr-11 15:04pm    
This is explained in detail in one of the references of my Solution -- please see.
You don't call, you dispatch the call and all data needed for a call to UI thread to be called later...
--SA
Sergey Alexandrovich Kryukov 19-Apr-11 15:03pm    
Correct, my 5, but see my more comprehensive Answer.
--SA
Abhinav S 20-Apr-11 0:02am    
Thanks SA.
Espen Harlinn 19-Apr-11 16:11pm    
Good link, my 5
You need to invoke the method on the ListView's UI thread, using Invoke (sync) or BeginInvoke (async). Assuming this code is run from within the owning form, this. should be ok, although listView1.Invoke is better imo.

Invoke takes a Delegate (MSDN docs[^]), and the version which doesn't take any extra parameters takes a void delegate.

Also, Invoke blocks the calling thread until the call finishes, which shouldn't be an issue for such a simple case, but just to let you know.

Try
listView1.Invoke( ()=>listView1.Items[0].SubItems[0].Text = (fff++).ToString() );


(NB: I haven't actually tried using Invoke with lambdas. You may have to cast it or write an actual function.)
 
Share this answer
 
Comments
Sandeep Mewara 19-Apr-11 13:44pm    
My 5!
Sergey Alexandrovich Kryukov 19-Apr-11 15:06pm    
This is correct, my 5, but please see my more comprehensive answer.
--SA
Espen Harlinn 19-Apr-11 16:10pm    
Nice and simple example, my 5!
Please see my recent Solution: cross threading event issue when i don't control the event[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 19-Apr-11 16:11pm    
Good link, my 5
Sergey Alexandrovich Kryukov 19-Apr-11 16:25pm    
Thank you, Espen.
--SA
Abhinav S 20-Apr-11 0:02am    
Good link 5.
Sergey Alexandrovich Kryukov 20-Apr-11 0:06am    
Thank you, Abhivav.
--SA

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