C# Timer Example

C# Timer Example: कई बार हमें ऐसी जरूरत पडती है कि निश्चित Time Duration पर हमें किसी Code को बार-बार Execute करना होता है। इस प्रकार की जरूरत को पूरा करने के लिए हम System.Threading.Timer Type को TimerCallback Delegate के साथ Use कर सकते हैं। इसे समझने के लिए हम निम्नानुसार एक Program Create कर सकते हैं:

File Name: TimerCallbacks.cs
using System;
using System.Threading;

namespace CSharpMultiThreading
{
    class Program
    {
        static void PrintTime(object state)
        {
            Console.WriteLine("Time is: {0}", DateTime.Now.ToLongTimeString());
        }

        static void Main(string[] args)
        {
            // Create the delegate for the Timer type.
            TimerCallback timeCB = new TimerCallback(PrintTime);

            // Establish timer settings.
            Timer t = new Timer(
            timeCB, 		// The TimerCallback delegate object.
            null, 		// Any info to pass into the called method (null for no info).
            0, 			// Amount of time to wait before starting (in milliseconds).
            1000); 		// Interval of time between calls (in milliseconds).
            Console.WriteLine("Hit key to terminate...");
            Console.ReadLine();
        }
    }
} 

// Output:
   Hit key to terminate...
   Time is: 2:26:10 PM
   Time is: 2:26:11 PM
   Time is: 2:26:12 PM
   . . .

इस Program में सबसे पहले हमने निम्नानुसार PrintTime() नाम का उस Method को Define किया है, जिसे हम निश्चित Time Duration पर बार-बार Execute करना चाहते हैं।

        static void PrintTime(object state)
        {
            Console.WriteLine("Time is: {0}", DateTime.Now.ToLongTimeString());
        }

फिर हमने Main() Method में निम्नानुसार TimerCallback Delegate Type का एक Object Create किया है और उसकी Invocation List में PrintTime() Method को Initialize कर दिया है:

// Create the delegate for the Timer type.
TimerCallback timeCB = new TimerCallback(PrintTime);

फिर हमने निम्नानुसार तरीके से Timer Type का एक Object t Create किया है और उसके Constructor के विभिन्न Parameters को निम्नानुसार तरीके से Specify किया है:

            Timer t = new Timer(
            timeCB, 		// The TimerCallback delegate object.
            null, 		// Any info to pass into the called method (null for no info).
            0, 			// Amount of time to wait before starting (in milliseconds).
            1000); 		// Interval of time between calls (in milliseconds).

Timer Type के Object के Constructor में पहले Parameter के रूप में हमें TimerCallback Delegate Object को Specify करना होता है, जिसकी Invocation List में हमारा वह Method Exist है, जिसे बार-बार Execute करना है।

जबकि दूसरे Parameter के रूप में हम Delegate को एक System.Object Type के Instance के रूप में Extra Data Pass कर सकते हैं। जबकि यदि हम कोई Data Pass न करना चाहें, तो दूसरे Argument के रूप में null Specify कर सकते हैं।

तीसरे Argument के रूप में हमें वह Time Specify करना होता है, जितने समय के बीत जाने के बाद हमें हमारे Method को Execute करना है। यहां हमने 0 Specify किया है, जो इस बात का Indication है कि हम हमारे Method को तुरन्त Run करना चाहते हैं।

तथा अन्तिम Parameter के रूप में हमें उस समयाविध को Specify करना होता है, जितना समय बीतने के बाद फिर उसे Specified Method को Invoke करना है। हमने हमारे इस Code में चौथे Parameter का मान 1000 Milliseconds रखा है, इसीलिए हर 1 Second बाद हमारा Code Execute होता है और हमें उपरोक्तानुसार Output प्राप्त होता है

यदि हम Timer Type के Object से Extra Data Delegate Object द्वारा Invoke होने वाले Method को Pass करना चाहते हैं, तो उस Data को Pass करने के लिए हम हमारे Time के Constructor को निम्नानुसार तरीके से Use कर सकते हैं:

Timer t = new Timer(timeCB,  “This is extra data.”, 0, 1000);

जब हम इस तरह से Data Pass करते हैं, तो उस Data को Accept करने के लिए हमें हमारे Callback Method को भी निम्नानुसार तरीके से Modify करना पडता है:

        static void PrintTime(object state)
        {
            Console.WriteLine("{0} Time is: {1}", state.ToString(), DateTime.Now.ToLongTimeString());
        }

Thread Synchronization
C# ThreadPool

C# in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook C#.NET in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

C#.NET in Hindi | Page:908 | Format: PDF

BUY NOW GET DEMO REVIEWS

MidRangeHub Finance Auto Loans Bank Business Health Insurance Insurance Investment Law Lif Insurance Loan Stocks how to endorse a check chase sapphire travel insurance chase sapphire travel delay when are property taxes due Tower Loans how to sell stocks on cash app Voided Check Examples Personal Finance Books Collateral Loans how to sell stocks on cashapp how do you sell your stocks on cash app how to sell stock on cash app joint account sofi joint account ally joint account capital one joint account best bank for joint account chase joint account cyber insurance coverage silverfort free cyber insurance coverage silverfort monjouro savings card Money6x Real Estate