0
Answer

Windows Service not calling Web Service appropriately

Matthew Cox

Matthew Cox

14y
2.8k
1

I am working on a project that requires creating a windows service that consumes a ASP.Net Web Service periodically. The web service polls XML documents, parses them, and stores the persistent data in a db (Sql Server 08). This isn't my preference for the design but work with me, I'm not the boss lol =P
The Windows Service uses asynchronous threads to consume the web service for polling different documents based on a timer. So far, if only one thread is active, then the service is consumed appropriately and the db has the data required. Once I introduce more threads then everything falls apart. I am brand new to programming windows services so even debugging (i.e attaching to a process) is new to me. I figure it is some type of race condition or synchronization issue. Any help or input would be great.
Note: No records are entered into the db with this version of the code.
 
public
partial class WinDriverService : ServiceBase
{
   private DatabaseHandler wthInfoDb, wthAlertDb ;
   private System.Timers.Timer wthInfoTimer;
   private System.Timers.Timer wthAlertTimer;
   public WinDriverService()
   {
      InitializeComponent();
      wthInfoDb =
new DatabaseHandler();
      wthAlertDb =
new DatabaseHandler();
   }
 
   protected override void OnStart(string[] args)
   {
      Thread wthInfoThread = new Thread(new ThreadStart(WthInfoThread));
      wthInfoThread.Start();
      Thread wthAlertThread = new Thread(new ThreadStart(WthAlertThread));
      wthAlertThread.Start();
   }
 
   private void WthInfoThread()
   {
      double interval = wthInfoDb.PollerConfig_ObtainTimerValue("Weather Info");
      interval = (interval > 0) ? interval * 60 * 1000 : 60000 * 45;
//either (mins * secs/min * ms/sec) or 45 mins
      wthInfoTimer =
new System.Timers.Timer(interval);
      wthInfoTimer.Elapsed +=
new ElapsedEventHandler(wthInfoTimer_Elapsed);
      wthInfoTimer.Start();
   }
 
   private void WthAlertThread()
   {
      double interval = wthAlertDb.PollerConfig_ObtainTimerValue("Weather Alerts");
      interval = (interval > 0) ? interval * 60 * 1000 : 60000 * 45;
//either (mins * secs/min * ms/sec) or 45 mins
      wthAlertTimer =
new System.Timers.Timer(interval);
      wthAlertTimer.Elapsed +=
new ElapsedEventHandler(wthAlertTimer_Elapsed);
      wthAlertTimer.Start();
   }
 
   private void wthInfoTimer_Elapsed(object sender, ElapsedEventArgs e)
   {
      try
      {
         PollerWebService.
PollingWebServiceSoapClient app = new global::PollerWebService.PollingWebServiceSoapClient();
         app.Open();
         PollerWebService.
PollerEntry[] entries = app.PollWeatherInfo();
         app.Close();
         if (entries.Length > 0)
         {
            TimelineController tlc = new TimelineController();
            tlc.ProcessNewEntries(entries);
         }
      }
      catch (Exception ee)
      {
      }
   }
 
   private void wthAlertTimer_Elapsed(object sender, ElapsedEventArgs e)
   {
      try
      {
         PollerWebService.
PollingWebServiceSoapClient app = new global::PollerWebService.PollingWebServiceSoapClient();
         app.Open();
         PollerWebService.
PollerEntry[] entries = app.PollWeatherAlerts();
         app.Close();
         if (entries.Length > 0)
         {
            TimelineController tlc = new TimelineController();
            tlc.ProcessNewEntries(entries);
         }
      }
      catch (Exception ee)
      {
      }
   }
 
   protected override void OnStop()
   {
      wthInfoTimer.Enabled =
false;
      wthAlertTimer.Enabled =
false;
   }
}