title: "Scheduler already exists issue" persists
body: <p>Even though I have used <em>Tarun Arora</em>'s <a href="http://geekswithblogs.net/TarunArora/archive/2013/01/19/quartz.net-windows-service-on-server-important-configuration-steps-to-remember.aspx" rel="nofollow">solution</a> to the </p>
  
  <blockquote>
    <p>Scheduler with the name xxx already exists.</p>
  </blockquote>
  
  <p>issue, I still get the same error. This is his solution and I assume he has already implemented the <em>Singleton</em> pattern correctly:</p>
  
  <pre><code>public class Scheduler
  {
      public readonly IScheduler Instance;
      public string Address { get; private set; }
      public string JobName { get; set; }
      public string JobGroup { get; set; }
      public int Priority { get; set; }
      public string CronExpression { get; set; }
  
      private readonly ISchedulerFactory _schedulerFactory;
  
  
      public Scheduler(string server, int port, string scheduler)
      {
          Address = string.Format("tcp://{0}:{1}/{2}", server, port, scheduler);
          _schedulerFactory = new StdSchedulerFactory(GetProperties(Address));
  
          try
          {
              Instance = _schedulerFactory.GetScheduler();
  
              if (!Instance.IsStarted)
                  Instance.Start();
          }
          catch (SchedulerException ex)
          {
              throw new Exception(string.Format("Failed: {0}", ex.Message));
          }
      }
  
      private static NameValueCollection GetProperties(string address) 
      {
          var properties = new NameValueCollection();
          properties["quartz.scheduler.instanceName"] = "ServerScheduler";
          properties["quartz.scheduler.proxy"] = "true";
          properties["quartz.threadPool.threadCount"] = "0";
          properties["quartz.scheduler.proxy.address"] = address;
          return properties;
      }
  
      public IScheduler GetScheduler()
      {
          return Instance;
      }
  }
  </code></pre>
  
  <p>I must confess I am not using the last part in which he says</p>
  
  <blockquote>
    <p>If you wanted to call Scheduler then you should be using something
    like the code sample below…</p>
  </blockquote>
  
  <pre><code>public SchedulerMetaData GetMetaData()
  {
          var scheduler = new Scheduler("ServerName", Convert.ToInt32("PortNumber"), "Scheduler"));
          return scheduler.GetMetaData();
  }
  </code></pre>
  
  <p>This how I am calling the scheduler in my application:</p>
  
  <pre><code>IScheduler sched = new Scheduler(logger, "127.0.0.1", 555, "QuartzScheduler").Instance;
  IJobDetail postbagjob = null;
  ITrigger postbagJobTrigger = null;
  try
  {
      postbagjob = JobBuilder.Create&lt;PostbagJob&gt;()
          .WithIdentity(jobName, jobGroup)
          .UsingJobData("CampaignId", campaignId.ToString())
          .UsingJobData("CampaignType", campaignType)
          .Build();
      postbagJobTrigger = (ICronTrigger)TriggerBuilder.Create()
          .WithIdentity(triggerName, triggerGroup)
          .WithCronSchedule(cron)
          .StartAt(DateTime.Now)
          .Build();
  }
  catch (SchedulerException ex)
  {
      MsgBox.Show(ex.Message);
  }
  </code></pre>
  
  <p>It works the first time I am about to add the job, but the second time I call it, <code>sched</code> becomes <code>null</code>.</p>