C# timer and thread use
How to deal with lag, multi-threading. Multithreading is easier to read than timer. Let's take a look at the relationship between timer and thread.
There are 3 timeser types
timer under. It is the timer that drags the control to the UI.
The source file is in this path C:\Windows\\Framework64\v4.0.30319
namespace { // Abstract: Implement a timer to raise events at user-defined time intervals. This timer is best used in Windows Forms applications and must be used in windows . [DefaultEvent("Tick")] [DefaultProperty("Interval")][SRDescriptionAttribute("DescriptionTimer")][ToolboxItemFilter("")] public class Timer : Component }
The startup timer code is as follows:
[SRCategory("CatBehavior")] [DefaultValue(false)] [SRDescription("TimerEnabledDescr")] public virtual bool Enabled { get { if ( == null) { return ; } return ; } set { lock () { if ( != value) { = value; if (!) { if (value) { if ( == null) { = new TimerNativeWindow(this); } = (this); (); } else { if ( != null) { (); } if () { (); } } } } } } }
Finally, the source code is called ();
It can be seen that the system's timer is finally called? The system has a timer. On Ucos, there are 32 timers, and of course you can also open threads.
They are different concepts. Windows is about the same. These timers should be CPU-related.
public void StartTimer(int interval) { if (this._timerID == 0 && !this._stoppingTimer && ()) { this._timerID = (int)(new HandleRef(this, ), ++, interval, ); } }
2. public sealed class Timer : MarshalByRefObject, IDisposable
public Timer(TimerCallback callback) { int dueTime = -1; int period = -1; StackCrawlMark stackCrawlMark = ; (callback, this, (uint)dueTime, (uint)period, ref stackCrawlMark); } [SecurityCritical] private void TimerSetup(TimerCallback callback, object state, uint dueTime, uint period, ref StackCrawlMark stackMark) { if (callback == null) { throw new ArgumentNullException("TimerCallback"); } this.m_timer = new TimerHolder(new TimerQueueTimer(callback, state, dueTime, period, ref stackMark)); } [SecurityCritical] internal static void Pause() { (); } [SecurityCritical] internal static void Resume() { (); }
Here is the operation of the TimerQueue queue. Since it is under the Threading namespace, it may be related to threads. The dll he is in is mscorlib.
3. , in. Just the right package.
[TimersDescription("TimerEnabled")] [DefaultValue(false)] public bool Enabled { get { return ; } set { if () { = value; = value; } else if () { = value; } else if ( != value) { if (!value) { if ( != null) { = null; (); = null; } = value; } else { = value; if ( == null) { if () { throw new ObjectDisposedException(().Name); } int num = (int)(); = new object(); = new (, , num, ? num : (-1)); } else { (); } } } } }
4. Use:
void Application_Start(object sender, EventArgs e) { // Code running at application startup if (timer != null) { (); (); timer = null; } int Interval = 3600000;//6 hours timer = new (Interval);//Ten minutes += SendSMS.Send_ticker; = Interval; = true; (); }
C# new thread delay
Open a new thread
In this thread, queue tasks.
After task 1 is completed, wait for the delay of 200ms, and then run task 2
private void Timer1_Tick(object sender, EventArgs e) { //throw new NotImplementedException(); (() => { ( new Action( () => { ("Into interrupt"+() + "\r\n"); })); //RS485.Set_io(7);//ok //RS485.Rest_io(7);//ok if (i > 8) i = 0; RS485.Set_io(i++);//ok (new Action(() => { ("First output" + () + "\r\n"); })); (200); RS485.Rest_io((ushort)(i - 2));//ok (new Action(() => { ("2nd output" + () + "\r\n"); })); (200); //RS485.Read_io_out(0,8);//ok RS485.Read_io_in(0, 8);//ok (new Action(() => { ("3rd output" + () + "\r\n"); })); //RS485.Read_io_Reg(0,4);// //RS485.Read_io_Regs(0, 6);// (200); }); }
The above is personal experience. I hope you can give you a reference and I hope you can support me more.