title: C# Best Way to run a function every second, Timer vs Thread?
body: <p>I am currently using a thread to run a function every second in a console application C# is a thread the best way to do it? As I have asked a lot of my friends and they have suggested using a timer not a thread for running something every second? If it is a better option to use a timer how would I go about running a function every second using a timer? I have looked around but I am really unsure if that applies to be and if its the right way of going about it in my own way. So can someone tell me the answer and how I can do it?</p>
  
  <p>So what is the best way to run it every second? Before you answer let me just let you know that this is running every second for like 2 days...
  My current thread coding</p>
  
  <pre><code>namespace Reality.Game.Misc
  {
      using Reality;
      using Reality.Communication.Outgoing;
      using Reality.Game.Rooms;
      using Reality.Game.Sessions;
      using Reality.Storage;
      using System;
      using System.Data;
      using System.Threading;
  
      public static class JobCacheWorker
      {
          private static Thread jWorkerThread;
  
          public static void HandleExpiration(Session Session)
          {
               using (SqlDatabaseClient client = SqlDatabaseManager.GetClient())
              {
                  Session.CharacterInfo.UpdateWorking(client, 0);
              }
          }
  
          public static void Initialize()
          {
              jWorkerThread = new Thread(new ThreadStart(JobCacheWorker.ProcessThread));
              jWorkerThread.Priority = ThreadPriority.Highest;
              jWorkerThread.Name = "JobCacheWorker";
              jWorkerThread.Start();
          }
  
          public static void CheckEffectExpiry(Session Session)
          {
              try
              {
                  //RunMyCodeHere...
              }
              catch (Exception exception)
              {
                      Console.WriteLine("Exception - JobCacheWorker -&gt; " + exception.Message);
              } 
          }
  
          private static void ProcessThread()
          {
              while (Program.Alive)
              {
                  try
                  {
                      foreach (Session Session in SessionManager.Sessions.Values)
                      {
                          if (Session != null &amp;&amp; Session.Authenticated &amp;&amp; !Session.Stopped)
                          {
                              CheckEffectExpiry(Session);
                              Thread.Sleep(1000);
                          }
                      }
                  }
                  catch (ThreadAbortException exception)
                  {
                      Output.WriteLine("ThreadAbortException - JobCacheWorker -&gt; " + exception.Message);
                  }
                  catch (ThreadInterruptedException exception2)
                  {
                      Output.WriteLine("ThreadInterruptedException - JobCacheWorker -&gt; " + exception2.Message);
                  }
              }
           }
      }
  }
  </code></pre>