Home News Best Way To Uninstall C # Using Timer In Windows Service

Best Way To Uninstall C # Using Timer In Windows Service

48
0

 

Recently, some users have seen an error message with C # regarding the use of a timer in a Windows service. There are a number of factors that can cause this problem. We will talk about this below.

 

 

System.Timers.Timer and System.Threading.Timer work for services.

Timers to avoid are System.Web.UI.Timer and System.Windows.Forms.Timer , which are for valid ASP and WinForms applications respectively … When you use them, the service loads an additional assembly that is not really needed for the type of application you are building.

Use System.Timers.Timer as in the following example (also be sure to use a class level variable to prevent garbage collection as pointed out in Tim Robinson’s answer):

  using System;using System.Timers;public class timer1{    System.Timers.Timer static private aTimer;    Public static void Main ()    {        // Usually the timer is declared at the class level.        // to keep it in scope for as long as needed.        // If the timer is declared in a long method,        // KeepAlive should be used to prevent JIT compiler        // avoid aggressive garbage collection        // to the end of the method. (See End of Method.)        //System.Timers.Timer aTimer;        // Create a timer every ten seconds.aTimer = new System.Timers.Timer (10000);        // Connect the Elapsed event to the timer.        aTimer.Elapsed   = new ElapsedEventHandler (OnTimedEvent);        // Set the interval to 2 seconds (2000 milliseconds).        aTimer.Interval = 2000;        aTimer.Enabled = true;        Console.WriteLine ("Press Enter to exit.");        Console.ReadLine ();        // If the timer is declared in a long method, use        // KeepAlive to prevent garbage collection        // to the end of the method.        //GC.KeepAlive(aTimer);    }}    // Indicate what to do when the event "expired"    // raised.    private static void OnTimedEvent (object source, ElapsedEventArgs e)    {        Console.WriteLine ("Timeout event occurred at {0}", e.SignalTime);    }}}}/ * This code example produces output similar to the following:Press Enter to exit the program.The past event occurred on May 20, 2007 at 20:42:27.The past event occurred on May 20, 2007 at 20:42:29.The past event occurred on May 20, 2007 at 20:42:31.... * / 

If you chose System.Threading.Timer , you can use:

  using System;using System.Threading;TimerExample class{    static void Main ()    {        AutoResetEvent autoEvent = new AutoResetEvent (false);        StatusChecker statusChecker = new StatusChecker (10);        // Create a delegate that calls metimer todes.        TimerCallback timerDelegate =            new TimerCallback (statusChecker.CheckStatus);        // Create a timer that signals the delegate to call it        // CheckStatus after one second and every 1/4 second        // after.        Console.WriteLine ("{0} Create timer.  N",            DateTime.Now.ToString ("h: mm: ss.fff"));        Timer stateTimer =                new timer (timerDelegate, autoEvent, 1000, 250);        // when autoEvent signals change period by        // 1/2 second.        autoEvent.WaitOne (5000, false);        stateTimer.Change (0, 500);        Console.WriteLine (" nEdit period.  N");        // When autoEvent signals a second time, eliminate it        // timer.        autoEvent.WaitOne (5000, false);        stateTimer.Dispose ();        Console.WriteLine (" n wipe timer.");    }}}}StatusChecker class{    int invokeCount, maxCount;    public StatusChecker (integer)    {        invokeCount = 0;        maxCount = number;    }}    // This method is called by the timer delegate.    public void CheckStatus (Object StateInfo)    {        AutoResetEvent autoEvent = (AutoResetEvent) stateInfo;        Console.WriteLine ("Check {0} status {1,2}.",            DateTime.Now.ToString ("h: mm: ss.fff"),            (   invokeCount) .ToString ());        if (invokeCount == maxCount)        {            // Reset the counter and tell Main.            invokeCount = 0;            autoEvent.Set ();        }}    }}}} 

Both notesThese are taken from the MSDN website.

What Is A Timer?

c# how to use timer in windows service

A timer is an object that can be used in your applications to trigger an event at a specific interval. There are three timer controls in the Microsoft .NET Framework. Each has its own nuances. There are three types of timers:

  • System.Windows.Forms.Timer – was designed for use in a single thread Windows Forms application. This is the same timer construct as in previous versions of Visual Basic. A common mistake is trying to use this timer in a Windows service application.
  • System.Timers.Timer is an updated version of the Windows Forms Timer, optimized for a server-side environment that does not have a user interface. This timer is ideal for use with Windows services.
  • System.Threading.Timer – was designed for use with ThreadPools. It uses callback methods instead of events, so it doesn’t rely on operating system support for timers. We will not cover this timer for the purposes of this article.

Add Your Solution Here