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:

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.

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?
Updated 26-May-11 4:43am

You need to use Control.Invoke to call a method on the UI thread - msdn documentation here[^].
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.
Sergey Alexandrovich Kryukov 26-May-11 22:17pm
I voted 5 by added links to my own explanation on how it works.
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[^],
Control events not firing after enable disable + multithreading[^].

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

see the below link

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