Click here to Skip to main content
Rate this: bad
good
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?
 
Regards
vasanth
Posted 10-Aug-12 9:47am
Comments
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.
--SA
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
good
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).
 
[EDIT]
 
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.
 
[END EDIT]
 
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 vb.net[^],
Control events not firing after enable disable + multithreading[^].
 
—SA
  Permalink  
v2
Comments
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.
 
--SA
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.
--SA
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.
--SA
losmac at 10-Aug-12 15:28pm
   
Excellent answer! +5
Sergey Alexandrovich Kryukov at 10-Aug-12 15:30pm
   
Thank you, Maciej.
--SA
CIDev at 10-Aug-12 16:14pm
   
Very fine answer, +5
Sergey Alexandrovich Kryukov at 10-Aug-12 16:36pm
   
Thank you.
--SA
Espen Harlinn at 11-Aug-12 4:56am
   
Good reply :-D
Sergey Alexandrovich Kryukov at 12-Aug-12 3:20am
   
Thank you, Espen.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
I think if u use multi thread programming , it'll work.
 
Good Luck
  Permalink  
Comments
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?
-SA
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 OriginalGriff 320
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 184
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 10 Aug 2012
Copyright © CodeProject, 1999-2014
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