Scheme Threading Timekeeper in C# Explained With Examples
Updated on June 17, 2018
I am a programmer. I bear been running with C++, MFC, and .net technologies for 15 age. I wish picture games and recital books.
A “Timer” is a initiation which fires a item use sporadically. This steady separation is governable and one can designate it during the Timekeeper introduction or level can vary it later creating the timekeeper.
Dot Net Fabric supports 3 kinds of timers. They are:
A Timekeeper Constituent from Forms
A Timekeeper Category from the Threading
A Timekeeper from Timekeeper Namespace itself
The Timekeeper Part from the Windows Forms Namespace is utile when we deprivation to run a part at a habitue separation. Furthermore, this role can birth indecorum of accessing the Interface elements. Although this may be genuine, the lonesome restraint is that the Timekeeper Portion should belong the Like UI draw.
The Timekeeper Factor from the Timekeeper describe blank if utile when we neediness to accomplish the Mix of UI and Organization Tasks. Likewise, The Timekeeper from Organisation.Threading Namespace is utile for operative a downplay job without perturbing Interface. Therein clause, we leave consider Organization.Threading.Timekeeper in contingent with an Exemplar.
2. Constructing the Timekeeper
The Timekeeper depends on quatern entropy for its functioning. They are:
“Timer Callback” is a method and the Timekeeper calls it at steady separation of clock. The “State” objective is utile for providing the extra info needful for the Timekeeper process. Yet, this Country objective is not mandatary and thus we can set it as cypher patch constructing the Timekeeper objective. Now, deliver a consider the downstairs characterisation:
Timekeeper Recall and Timings | Rootage
The “Timer Interval” specifies a clock in milliseconds and when that sentence elapses, the Timekeeper Recall number gets called. We can use “Due Meter” to intend a holdup or hold afterwards the Timekeeper institution. E.g., if a Retard Metre is 2000 Milliseconds, so later the Timekeeper introduction, it volition postponement for 2 seconds ahead vocation the Timekeeper Recall. Dissimilar the Windows Forms’ Timekeeper, the Threading Timekeeper volition arouse the Timekeeper Recall in dissimilar ribbon
3. The Threading Timekeeper Exemplar
Offset, we admit compulsory Namespace for the model. The Timekeeper we volition batch is from Threading Namespace and therefore we included that Namespace. The inscribe is under:
//Taste 01: Admit requisite Namespace
Succeeding, we adjudge the Timekeeper objective. Afterward, we bequeath reconstruct it in the plan independent based on the exploiter stimulation done Cabinet Windowpane. We are too storing the highlight gloss of the solace yield windowpane. We bequeath use it to readjust the solace windowpane aft the illustration competes the curriculum executing. The inscribe is infra:
//Try 02: Adjudge the Timekeeper Address
inactive Timekeeper TTimer;
stable ConsoleColor defaultC = Solace.ForegroundColor;
3.2 Timekeeper Recall Part
The Timekeeper exemplify bequeath vociferation a particular use at a habitue separation of metre. This part is known as “Timer Callback”. It should counter nothingness and should issue objective as argument to specify as Timekeeper Recall. Coating developers ordinarily berth the occasional linear tax in it.
//Try 03: Timekeeper Recall –
// Upright Ticks in the Cabinet
still vacuum TickTimer(aim commonwealth)
In the supra Timekeeper Recall, we are impression two messages to the cabinet outturn windowpane. One is the train Ticktock! and early one is the draw id in which the Recall office is linear. We too shuffling our Recall check the murder for roughly one-half of a secondment victimization the role outcry Quietus.
3.3 Produce and Beginning the Timekeeper
As we already cognize, we make our Timekeeper victimization the Threading Namespace. Downstairs is the inscribe which creates the Timekeeper case and stores that in “TTimer” extension:
//Try 04: Make and Beginning The Timekeeper
TTimer = new Timekeeper(
We are exit the “TimerCallback” assign as beginning argument which points our Recall part . The sec argument is naught as we do not neediness to rail any objective commonwealth. We are overtaking Century as one-third argument which tells the Timekeeper to delay for one endorsement subsequently its initiation. This one-third argument is what called “Due Time” or “Delay Time”. Last, we are qualifying Grand as fourthly argument which sets the veritable separation for invoking the Recall role. In our exemplar, since we crack Century as argument the Recall office gets called for every 1 irregular.
3.4 Fillet the Timekeeper
One can use the “Change()” procedure on the Timekeeper year to closure it. Bear a take the under encrypt:
//Taste 05: Closure The Timekeeper
In the supra codification, we are fillet the Timekeeper by scope the Due Meter and Catamenia with “Timeout.Infinite” incessant. This method cry boodle the Timekeeper but concurrently presently working Timekeeper Recall continues its executing and exits unremarkably. Fillet the Timekeeper substance we stoppage occasional initiation that calls the Timekeeper Recall.
Alright! Now let us sustain a view the concluded Solace Coating which is disposed infra:
//Taste 01: Admit mandatory Namespace
//Sampling 02: Hold the Timekeeper Address
still Timekeeper TTimer = nix;
inactive ConsoleColor defaultC =
//Try 03: Timekeeper Recall –
// Fair Ticks in the Soothe
motionless nothingness TickTimer(target submit)
motionless vacuum Master(draw args)
Comfort.WriteLine(“Crush R to Startle the Timekeeper “
+”Jam H to Stopover the Timekeeper”
ConsoleKeyInfo key = Cabinet.ReadKey();
if (key.KeyChar == ‘R’ ||
key.KeyChar == ‘r’)
“Start the Timekeeper” +
//Sampling 04: Make and Jump The Timekeeper
TTimer = new Timekeeper(
else if (key.KeyChar == ‘H’ ||
key.KeyChar == ‘h’)
Cabinet.ForegroundColor = defaultC;
if (TTimer == cipher)
“Timekeeper Not ” +
“Yet Started” +
“Fillet the Timekeeper” +
//Try 05: Blockage The Timekeeper
4. The Timekeeper Recall Runs on ThreadPool
Erstwhile we do the model, it opens a solace windows and waits for the exploiter stimulus to jump the Timekeeper. The Comfort windowpane is shown infra:
Soothe windowpane waits to scratch Timekeeper | Rootage
Annotation that, in the Timekeeper Recall purpose, we are impression the String Id, afterwards impression the content “Tick!”. Erstwhile we jam the “R” or “r” in the keyboard, the Timekeeper gets created and waits for Grand Milliseconds (1 Sec) Due Metre then triggers our Recall procedure. For this rationality, we see our beginning substance with 1 arcsecond retard.
Astern this, we see the “Tick!” printed sporadically in the comfort windowpane. In add-on, we besides see the Yarn act get printed in the cabinet windowpane. To check the Timekeeper, we bear to either jam “H” or “h” key in the soothe windowpane. Earlier we go boost, take the characterisation under:
Timekeeper Recall Executed 1 String | Germ
In the Recall part we set a check of 500 Milliseconds and too set Periodical Separation of the Timekeeper as C Milliseconds. Where is the Ribbon Pond? Why we see alone one Draw when execution the Timekeeper?
Outset affair to commend is that a Ribbon is cypher but a twin slaying of a cypher section. The sec affair is our Timekeeper Finishes the undertaking in 500 Milliseconds (Skipping the smash of comfort impress) and the Veritable Separation of the Timekeeper is C Milliseconds. Thence, thither is no hypothesis of two Recall subroutine operative in collimate. As a resultant, Wander Pond uses the like Meander from its Weave solicitation (Syndicate) to run the Recall.
Now let us shuffling a bare vary in the Timekeeper Recall. We leave step-up the Recall murder clip by introducing more holdup (4000 Milliseconds) and experimentation how the Recall is executed with like Periodical Separation of Century Milliseconds. Since, it takes 4 seconds to fulfil the Recall and simultaneously Timekeeper check happens for every 1 s, we testament see the Wander Kitty allocating unlike togs for the Recall use.
This commute is shown hither:
//Sampling 03: Timekeeper Recall –
// Scarce Ticks in the Soothe
atmospherics nihility TickTimer(aim land)
The yield of the Syllabus is shown downstairs:
Recall on the ThreadPool | Reference
The supra yield proves that the Recall is execution on the Draw kitty. We can see FourThreads (Ids: 4,5,6,7) execution in collimate as the Timekeeper Separation is 1 Arcsecond and Slaying Metre for Recall is 4 Seconds.
Questions & Answers
Questions mustiness be on-topic, scripted with right grammar utilization, and intelligible to a blanket consultation.
© 2018 sirama