Click here to Skip to main content
15,032,273 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,

I'm trying to fire a method thats running on Threading.Timer. The method performs a few calculations then displays the results in a listView control.

Example:
C#
Threading.Timer thrdTimer = new Threading.Timer(myMethod,null,0,1000);

private void myMethod(object obj)
{
   // do some calculations...
   // pass calculation values(val1,val2,val3) to listView

   listView.Items[0].SubItems[1].Text = val1;
   listView.Items[1].SubItems[2].Text = val2;
   listView.Items[2].SubItems[3].Text = val3;
}

How do I go about invoking the listView?
Posted
Updated 26-May-11 4:43am
v4

You need to use Control.Invoke to call a method on the UI thread - msdn documentation here[^].
   
Comments
d.allen101 26-May-11 10:18am
   
thanks, i've read the article a couple of times still not quite getting it.
Sergey Alexandrovich Kryukov 26-May-11 22:16pm
   
See my answer then. Better now? If not, ask your questions.
--SA
Sergey Alexandrovich Kryukov 26-May-11 22:17pm
   
I voted 5 by added links to my own explanation on how it works.
--SA
Abhinav S 27-May-11 0:21am
   
Thank you SA. I read your answer - hope this helps Donald.
Hope I'll explain invocation just a bit better than MSDN on the page Abhinav referenced:

Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

I cannot believe you really need timer. In real life in most cases you should better use threading. For example, if you need to poll something periodically, create a thread and call what you want in cycle calling Thread.Sleep for timing.

Please see this link to my past answers:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
   
Comments
Abhinav S 27-May-11 0:20am
   
Good links. My 5.
   
Thank you, Abhinav.
--SA
You should use BackGroundWorkder Component to perform this kind of task...

see the below link
Link1[^]
Link2[^]
Link3[^]
   

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