0
Reply

Windows Services written in C# hangs

Mohith Kumar

Mohith Kumar

Mar 17 2009 3:31 AM
2.3k
Hello All,

I have 3 windows services written in C#. I use them to process files from a folder into database and further to process the data into various SQL Tables.

All the services hang atleast once in a week stopping the entire process. The
files are queued up in the folders waiting to be processed. The services in the Service Control Manager seems to be running but they do not process any files. But when the processes are restarted, they run just fine until any one
hangs again. The following error is written to Event Viewer by Service Control Manager:

The [Service Name] service terminated unexpectedly. It has done this XX
time(s). The following corrective action will be taken in 0 milliseconds: Restart Service.

Any help would be appreciated. (find code below)


using System;
using System.IO ;
using System.Threading ;
using System.Xml ;

namespace XXX.ABCD.Dls.Queue
{
///
/// A comparison is done between the two Data objects, by individual
entities
/// This means only entities that have changed will be updated
/// this causes less SQL interaction and speeds the whole process up
///

public class QueueProcess
{

// Two XML documents to hold the queue and previous data
private XmlDocument xmlQueue = new XmlDocument();
private XmlDocument xmlPrevious = new XmlDocument();

// A data queue object to process information from the Queue
private DataQueue oDataQueue = new DataQueue() ;
// A data previous object to get the last XML processed from the database
private DataPrevious oDataPrevious = new DataPrevious() ;

public const int status_begin = 0 ;
public const int status_end = 999 ;

private bool stopped = false ;

// Event argument instances
private QueueProcess_Status_EventArgs eQueueProcess_Status = new
QueueProcess_Status_EventArgs() ;

#region Public

// Constructor
public QueueProcess()
{
}

// Make the link between the delegate function and the place in code where
it is
called
public event QueueProcess_Status QueueProcess_Status_Event ;

///
/// Begin polling the folders in the path_incoming for ZIP files
///
///

public void Start()
{
Status_Event(status_begin, "Queue Thread Begin") ;
stopped = false ;

while (true)
{
Thread.Sleep(100) ;
Start_QueueProcess() ;
if (stopped) break ;
}
Status_Event(status_end, "Queue Thread End") ;

}

public void Stop()
{
stopped = true ;
}

#endregion

#region Private functions

///
/// Method to Start processing QueueProcess
///

private void Start_QueueProcess()
{
// Process the Queue
try
{
oDataQueue.GetNext() ;
// if it is valid process it
if (oDataQueue.QueueID != 0)
{
try
{

#region Load the XML from the Queue and create a ComputerNew object
// Get the info from the XML
try
{
xmlQueue.LoadXml(oDataQueue.INVData) ;
}
catch
{
xmlQueue.LoadXml("") ;
}
ComputerNew oComputerNew = new ComputerNew(oDataQueue.AccountID , ref
xmlQueue);
#endregion

// If AccountID == 0. This means an error has occured loading computer
new
if (oComputerNew.AccountID != 0)
{
#region Get the XML from the previous data in the DB and Create a
ComputerPrevious object
oDataPrevious.Get(oComputerNew.AccountID , oComputerNew.NetBIOSName);

// Get the info from the XML
try
{
xmlPrevious.LoadXml(oDataPrevious.INVData) ;
}
catch
{
xmlPrevious.LoadXml("") ;
}

ComputerNew oComputerPrevious = null ;
try
{
oComputerPrevious = new ComputerNew(0 , ref xmlPrevious) ;
}
catch
{
Status_Event(107,"Error : XML - Could not create computer previous
object") ;
}
#endregion

#region Compare the New and Previous Prepared XML and if different
build the collection of data
// If different then call prepare data on entity new to populate
collection
if (oComputerNew.customNew.PreparedXml != oComputerPrevious.customNew.
PreparedXml )
oComputerNew.customNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

if (oComputerNew.summaryNew.PreparedXml != oComputerPrevious.
summaryNew.PreparedXml )
oComputerNew.summaryNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

if (oComputerNew.displayNew.PreparedXml != oComputerPrevious.
displayNew.PreparedXml )
oComputerNew.displayNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

if (oComputerNew.driveNew.PreparedXml != oComputerPrevious.driveNew.
PreparedXml )
oComputerNew.driveNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

if (oComputerNew.networkNew.PreparedXml != oComputerPrevious.
networkNew.PreparedXml )
oComputerNew.networkNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

if (oComputerNew.printerNew.PreparedXml != oComputerPrevious.
printerNew.PreparedXml )
oComputerNew.printerNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

if (oComputerNew.shortcutNew.PreparedXml != oComputerPrevious.
shortcutNew.PreparedXml )
oComputerNew.shortcutNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

if (oComputerNew.soeNew.PreparedXml != oComputerPrevious.soeNew.
PreparedXml )
oComputerNew.soeNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

if (oComputerNew.softwareNew.PreparedXml != oComputerPrevious.
softwareNew.PreparedXml )
oComputerNew.softwareNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

if (oComputerNew.usbNew.PreparedXml != oComputerPrevious.usbNew.
PreparedXml )
oComputerNew.usbNew.PrepareData(ref xmlQueue) ; // This sets
updaterequired in entity

#endregion

ComputerCurrent oComputerCurrent = new ComputerCurrent(oComputerNew);

if (oComputerCurrent.Compare())
{
try
{
//Now update the Previous Data from the data queue object
oDataPrevious.INVData = oDataQueue.INVData ;
//Set the ID of the entity as the ComputerID
oDataPrevious.ID = oComputerCurrent.ID;
oDataPrevious.Insert();
}
catch
{
Status_Event(108,"Error : Could not update previous data") ;
}

try
{
oDataQueue.Delete();
}
catch
{
Status_Event(101,"Error : Could not Delete DataQueue item") ;
}

}
else
{
try
{
oDataQueue.Failed();
Status_Event(102,"Error : Failed to process queue entry") ;
}
catch
{
Status_Event(103,"Error : Could not Mark DataQueue Failed") ;
}
}

}
else // error creating computernew. No exception AccountID set to 0
{
try
{
oDataQueue.Failed();
Status_Event(105,"Error : XML - Could not create computer new object")
;
}
catch
{
Status_Event(106,"Error : Could not Mark DataQueue Failed") ;
}
}

}
catch
{
Status_Event(104,"Error : Could not create Computer New Object") ;
}

}

}
catch (Exception ex)
{
Status_Event(100,"Error : Could not get next item in DataQueue") ;
Status_Event(100,"Exception : " + ex.Message ) ;
}
}

private void Status_Event(int code , string message)
{
int codeoffset = 2000 ;
code += codeoffset ;
eQueueProcess_Status.StatusCode = code ;
eQueueProcess_Status.StatusMessage = message ;
QueueProcess_Status_Event(this,eQueueProcess_Status) ;
}

#endregion

#region Properties

public bool Stopped
{
get
{
return stopped ;
}
}

#endregion

#region Event Classes and Delegates

#region Status Event Class

public delegate void QueueProcess_Status( object sender,
QueueProcess_Status_EventArgs e) ;

public class QueueProcess_Status_EventArgs : EventArgs
{
#region Private Variables

private int statuscode ;
private string statusmessage ;

#endregion

#region Public Properties

public int StatusCode
{
get { return statuscode ; }
set { statuscode = value ; }
}

public string StatusMessage
{
get { return statusmessage ; }
set { statusmessage = value ; }
}

#endregion
}

#endregion

#endregion

}
}