Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hello
i want a code or .DLL of the changing and moving of the value of the label with timer(every second),
i want to use this label on my form so as to show me the minimum of the stock of every product,
please if somebody have any idea , help me.
thanks everybody.:thumbsup:
Posted

The timer System.Windows.Forms.Timer should never be used, especially in animation (see also my comments to the Answer by Piccadily). It's better to use System.Timers.Timer. However, all timers are problematic: did you think what happens if the code is still busy with timer's event handle when a new event is fired. Resolvable, I understand, still an invitation to instability. Best method is a thread with System.Threading.Thread.Sleep calls.

Accuracy is not a big problem as the move of the label can be done not per number of event (not fixed shift), but proportionally to the really elapsed time (measured using System.DateTime.Now). UI cannot be controlled from within the non-UI thread. You should use BeginInvoke of Invoke method of Control or System.Windows.Threading.Dispatcher. (Actually, same thing for System.Timers.Timer.)

See for more detail: Invocation: Control.Invoke() vs. Control.BeginInvoke()[^], Problem with Treeview Scanner And MD5[^].

Useful code and explanation for a thread wrapper: How to pass ref parameter to the thread[^].

A thread can be used in three ways: 1) a regular thread created by the constructor of System.Threading.Thread (see the reference on thread wrapper above); 2) a thread from a thread pool; 3) System.ComponentModel.BackgroundWorker.

—SA
 
Share this answer
 
Comments
Piccadilly Yum Yum 6-Mar-11 11:07am    
Excuse me ... what does it means "SA" ?
Sergey Alexandrovich Kryukov 6-Mar-11 12:29pm    
OK. This is my nick, signature and how many people call me, from "Sergey A Kryukov".
--SA
Sergey Alexandrovich Kryukov 6-Mar-11 12:30pm    
Excuse me, wasn't that you who voted "1"?
This is very usual though.
--SA
C#
Timer t;
        public fMain()
        {
            InitializeComponent();
            t = new Timer();
            t.Interval = 1000; // milliseconds
            t.Tick += new System.EventHandler(OnTimerEvent);
            t.Enabled = true;
            t.Start();
        }
        public static void OnTimerEvent(object source, EventArgs e)
        {
             // do what you like with your label ...
        }
 
Share this answer
 
Comments
fjdiewornncalwe 5-Mar-11 12:05pm    
I do so hate using the timer, but in this case I think this is the required solution. This sounds like a homework assignment and my guess is that this is the type of solution the instructor would be looking for. (+5)
Sergey Alexandrovich Kryukov 5-Mar-11 21:19pm    
This is bad, because timer is bad, by many reasons (Marcus knows). Now, Marcus or anyone might say that the timer is required because the time accuracy is needed better then with thread (which would be the best solution). But, look at that timer! As it is not mentioned otherwise, this is a System.Windows.Forms.Timer. This timer is much worse than anything else, no matter timer or thread with Sleep. It should never be uses anywhere, especially in animation.
So, sorry, my 3 this time.
--SA
P.S.: and of course I don't care a bit if this is a school requirement. I want to help learning, not grades (unfortunately, in certain cases, the goals of school grades are directly opposite to learning).
--SA
Sergey Alexandrovich Kryukov 5-Mar-11 21:34pm    
I added my Answer, few good references and arguments.
--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