Click here to Skip to main content
11,795,848 members (82,321 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C#
hai guys,

In my project i am using 3 different timer control for move 3 different listviews items one by one... so my application shows very slow process. I can't able to click other button, After stopped the timer only other controls are work without time gap.

How can i resolve this problem? Is there any solutions?

Posted 10-Aug-12 8:47am
Wes Aday at 10-Aug-12 14:52pm
Sounds more like you have a bad application design.
Mika Wendelius at 10-Aug-12 14:59pm
I agree that this sounds like bad design. Could you explain what you're trying to achieve. Why the timers are used to move items etc.
Sergey Alexandrovich Kryukov at 10-Aug-12 15:13pm
I agree, too, but I provided some general advice -- please see.
vasanthkumarmk at 10-Aug-12 15:03pm
Just i have list view control contains some 20 items, when i click the button the top most item will select and after few seconds that selection will move to next, like that it will move up to end of the list one by one.

Thats why i am using timer to selection moves.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Timers are generally bad. One timer, System.Windows.Forms.Timer is the easiest but provide unacceptable accuracy (there are rare cases when that's all right, but not like yours), other two are better, but there is a lot of bothering with them. Did you ever try to account for the situation when a new timer event is invoked when your timer event handler is still executing?

Threads is much more straightforward way, because the logic is linear. You need to create a separate thread with a loop with some delay using Thread.Sleep.

First of all, I recommend using a thread wrapper to solve the problem of passing parameters to it. Please see my past solutions:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

Now, you need to update your UI in each cycle. On problem is: you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).


By the way, two "normal" timers (other than System.Windows.Forms.Timer) will need UI thread invocation described above anyway, so you cannot win with a timer by not doing the invocation.


You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in[^],
Control events not firing after enable disable + multithreading[^].

Mika Wendelius at 10-Aug-12 15:18pm
Good points! I still wonder why the three timers etc. One reason could be to create a small animation, but that would explain the amount of the timers...
Sergey Alexandrovich Kryukov at 10-Aug-12 15:23pm
I think can answer to this, more or less.

System.Timers.Timer is just a little wrapper around System.Threading.Timer, for convenience; and this one is the most reasonable. And System.Windows.Forms.Timer is created for the weak-minded, mostly. :-) For Microsoft, it always has been one of the most important priority to please such people. One benefit of it: the event is executed in UI thread, so no UI invocation is required (it is needed for "normal" timer anyway). The accuracy of this timer is simply amazing. I know only one use of it: a big one-time delay in UI, such as splash screen.

Mika Wendelius at 10-Aug-12 15:28pm
That's a fair guess. And it seems that the OP clarified the situation a bit.
Sergey Alexandrovich Kryukov at 10-Aug-12 15:29pm
Hope so; thank you, Mika.
Sergey Alexandrovich Kryukov at 10-Aug-12 15:28pm
By the way, the small (or big) animation with timers is even worse, so it should use threading. The timers are still needed in some cases.
losmac at 10-Aug-12 15:28pm
Excellent answer! +5
Sergey Alexandrovich Kryukov at 10-Aug-12 15:30pm
Thank you, Maciej.
CIDev at 10-Aug-12 16:14pm
Very fine answer, +5
Sergey Alexandrovich Kryukov at 10-Aug-12 16:36pm
Thank you.
Espen Harlinn at 11-Aug-12 4:56am
Good reply :-D
Sergey Alexandrovich Kryukov at 12-Aug-12 3:20am
Thank you, Espen.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


I think if u use multi thread programming , it'll work.

Good Luck
vasanthkumarmk at 10-Aug-12 15:04pm
But how?
Sergey Alexandrovich Kryukov at 10-Aug-12 15:12pm
How? Please see my answer. What else would you need to get explained?
s7_roshan at 10-Aug-12 15:16pm
I'm sorry, i don't know how it is possible. i'm not a professional developer, Just as my teacher told us before, sometimes, using multi threading may work.
losmac at 10-Aug-12 15:29pm
Use links provided by Sergey (SA).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 410
1 Abhinav S 368
2 OriginalGriff 300
3 Sergey Alexandrovich Kryukov 266
4 KrunalRohit 249
0 OriginalGriff 1,852
1 Maciej Los 1,465
2 KrunalRohit 1,300
3 CPallini 840
4 Richard MacCutchan 828

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 10 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100