Hello All,
We have 3 windows services written in C#. We 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)
[code=C#]
using System;
using System.IO ;
using System.Threadin g ;
using System.Xml ;
namespace XXX.ABCD.Dls.Qu eue
{
/// <summary>
/// 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
/// </summary>
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_St atus_EventArgs eQueueProcess_S tatus = new
QueueProcess_St atus_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_St atus QueueProcess_St atus_Event ;
/// <summary>
/// Begin polling the folders in the path_incoming for ZIP files
///
/// </summary>
public void Start()
{
Status_Event(st atus_begin, "Queue Thread Begin") ;
stopped = false ;
while (true)
{
Thread.Sleep(10 0) ;
Start_QueueProc ess() ;
if (stopped) break ;
}
Status_Event(st atus_end, "Queue Thread End") ;
}
public void Stop()
{
stopped = true ;
}
#endregion
#region Private functions
/// <summary>
/// Method to Start processing QueueProcess
/// </summary>
private void Start_QueueProc ess()
{
// Process the Queue
try
{
oDataQueue.GetN ext() ;
// if it is valid process it
if (oDataQueue.Que ueID != 0)
{
try
{
#region Load the XML from the Queue and create a ComputerNew object
// Get the info from the XML
try
{
xmlQueue.LoadXm l(oDataQueue.IN VData) ;
}
catch
{
xmlQueue.LoadXm l("<XXXX/>") ;
}
ComputerNew oComputerNew = new ComputerNew(oDa taQueue.Account ID , ref
xmlQueue);
#endregion
// If AccountID == 0. This means an error has occured loading computer
new
if (oComputerNew.A ccountID != 0)
{
#region Get the XML from the previous data in the DB and Create a
ComputerPreviou s object
oDataPrevious.G et(oComputerNew .AccountID , oComputerNew.Ne tBIOSName);
// Get the info from the XML
try
{
xmlPrevious.Loa dXml(oDataPrevi ous.INVData) ;
}
catch
{
xmlPrevious.Loa dXml("<XXXX/>") ;
}
ComputerNew oComputerPrevio us = null ;
try
{
oComputerPrevio us = new ComputerNew(0 , ref xmlPrevious) ;
}
catch
{
Status_Event(10 7,"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.c ustomNew.Prepar edXml != oComputerPrevio us.customNew.
PreparedXml )
oComputerNew.cu stomNew.Prepare Data(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.s ummaryNew.Prepa redXml != oComputerPrevio us.
summaryNew.Prep aredXml )
oComputerNew.su mmaryNew.Prepar eData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.d isplayNew.Prepa redXml != oComputerPrevio us.
displayNew.Prep aredXml )
oComputerNew.di splayNew.Prepar eData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.d riveNew.Prepare dXml != oComputerPrevio us.driveNew.
PreparedXml )
oComputerNew.dr iveNew.PrepareD ata(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.n etworkNew.Prepa redXml != oComputerPrevio us.
networkNew.Prep aredXml )
oComputerNew.ne tworkNew.Prepar eData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.p rinterNew.Prepa redXml != oComputerPrevio us.
printerNew.Prep aredXml )
oComputerNew.pr interNew.Prepar eData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.s hortcutNew.Prep aredXml != oComputerPrevio us.
shortcutNew.Pre paredXml )
oComputerNew.sh ortcutNew.Prepa reData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.s oeNew.PreparedX ml != oComputerPrevio us.soeNew.
PreparedXml )
oComputerNew.so eNew.PrepareDat a(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.s oftwareNew.Prep aredXml != oComputerPrevio us.
softwareNew.Pre paredXml )
oComputerNew.so ftwareNew.Prepa reData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.u sbNew.PreparedX ml != oComputerPrevio us.usbNew.
PreparedXml )
oComputerNew.us bNew.PrepareDat a(ref xmlQueue) ; // This sets
updaterequired in entity
#endregion
ComputerCurrent oComputerCurren t = new ComputerCurrent (oComputerNew);
if (oComputerCurre nt.Compare())
{
try
{
//Now update the Previous Data from the data queue object
oDataPrevious.I NVData = oDataQueue.INVD ata ;
//Set the ID of the entity as the ComputerID
oDataPrevious.I D = oComputerCurren t.ID;
oDataPrevious.I nsert();
}
catch
{
Status_Event(10 8,"Error : Could not update previous data") ;
}
try
{
oDataQueue.Dele te();
}
catch
{
Status_Event(10 1,"Error : Could not Delete DataQueue item") ;
}
}
else
{
try
{
oDataQueue.Fail ed();
Status_Event(10 2,"Error : Failed to process queue entry") ;
}
catch
{
Status_Event(10 3,"Error : Could not Mark DataQueue Failed") ;
}
}
}
else // error creating computernew. No exception AccountID set to 0
{
try
{
oDataQueue.Fail ed();
Status_Event(10 5,"Error : XML - Could not create computer new object")
;
}
catch
{
Status_Event(10 6,"Error : Could not Mark DataQueue Failed") ;
}
}
}
catch
{
Status_Event(10 4,"Error : Could not create Computer New Object") ;
}
}
}
catch (Exception ex)
{
Status_Event(10 0,"Error : Could not get next item in DataQueue") ;
Status_Event(10 0,"Exception : " + ex.Message ) ;
}
}
private void Status_Event(in t code , string message)
{
int codeoffset = 2000 ;
code += codeoffset ;
eQueueProcess_S tatus.StatusCod e = code ;
eQueueProcess_S tatus.StatusMes sage = message ;
QueueProcess_St atus_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_St atus( object sender,
QueueProcess_St atus_EventArgs e) ;
public class QueueProcess_St atus_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
}
}
[/code]
We have 3 windows services written in C#. We 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)
[code=C#]
using System;
using System.IO ;
using System.Threadin g ;
using System.Xml ;
namespace XXX.ABCD.Dls.Qu eue
{
/// <summary>
/// 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
/// </summary>
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_St atus_EventArgs eQueueProcess_S tatus = new
QueueProcess_St atus_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_St atus QueueProcess_St atus_Event ;
/// <summary>
/// Begin polling the folders in the path_incoming for ZIP files
///
/// </summary>
public void Start()
{
Status_Event(st atus_begin, "Queue Thread Begin") ;
stopped = false ;
while (true)
{
Thread.Sleep(10 0) ;
Start_QueueProc ess() ;
if (stopped) break ;
}
Status_Event(st atus_end, "Queue Thread End") ;
}
public void Stop()
{
stopped = true ;
}
#endregion
#region Private functions
/// <summary>
/// Method to Start processing QueueProcess
/// </summary>
private void Start_QueueProc ess()
{
// Process the Queue
try
{
oDataQueue.GetN ext() ;
// if it is valid process it
if (oDataQueue.Que ueID != 0)
{
try
{
#region Load the XML from the Queue and create a ComputerNew object
// Get the info from the XML
try
{
xmlQueue.LoadXm l(oDataQueue.IN VData) ;
}
catch
{
xmlQueue.LoadXm l("<XXXX/>") ;
}
ComputerNew oComputerNew = new ComputerNew(oDa taQueue.Account ID , ref
xmlQueue);
#endregion
// If AccountID == 0. This means an error has occured loading computer
new
if (oComputerNew.A ccountID != 0)
{
#region Get the XML from the previous data in the DB and Create a
ComputerPreviou s object
oDataPrevious.G et(oComputerNew .AccountID , oComputerNew.Ne tBIOSName);
// Get the info from the XML
try
{
xmlPrevious.Loa dXml(oDataPrevi ous.INVData) ;
}
catch
{
xmlPrevious.Loa dXml("<XXXX/>") ;
}
ComputerNew oComputerPrevio us = null ;
try
{
oComputerPrevio us = new ComputerNew(0 , ref xmlPrevious) ;
}
catch
{
Status_Event(10 7,"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.c ustomNew.Prepar edXml != oComputerPrevio us.customNew.
PreparedXml )
oComputerNew.cu stomNew.Prepare Data(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.s ummaryNew.Prepa redXml != oComputerPrevio us.
summaryNew.Prep aredXml )
oComputerNew.su mmaryNew.Prepar eData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.d isplayNew.Prepa redXml != oComputerPrevio us.
displayNew.Prep aredXml )
oComputerNew.di splayNew.Prepar eData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.d riveNew.Prepare dXml != oComputerPrevio us.driveNew.
PreparedXml )
oComputerNew.dr iveNew.PrepareD ata(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.n etworkNew.Prepa redXml != oComputerPrevio us.
networkNew.Prep aredXml )
oComputerNew.ne tworkNew.Prepar eData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.p rinterNew.Prepa redXml != oComputerPrevio us.
printerNew.Prep aredXml )
oComputerNew.pr interNew.Prepar eData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.s hortcutNew.Prep aredXml != oComputerPrevio us.
shortcutNew.Pre paredXml )
oComputerNew.sh ortcutNew.Prepa reData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.s oeNew.PreparedX ml != oComputerPrevio us.soeNew.
PreparedXml )
oComputerNew.so eNew.PrepareDat a(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.s oftwareNew.Prep aredXml != oComputerPrevio us.
softwareNew.Pre paredXml )
oComputerNew.so ftwareNew.Prepa reData(ref xmlQueue) ; // This sets
updaterequired in entity
if (oComputerNew.u sbNew.PreparedX ml != oComputerPrevio us.usbNew.
PreparedXml )
oComputerNew.us bNew.PrepareDat a(ref xmlQueue) ; // This sets
updaterequired in entity
#endregion
ComputerCurrent oComputerCurren t = new ComputerCurrent (oComputerNew);
if (oComputerCurre nt.Compare())
{
try
{
//Now update the Previous Data from the data queue object
oDataPrevious.I NVData = oDataQueue.INVD ata ;
//Set the ID of the entity as the ComputerID
oDataPrevious.I D = oComputerCurren t.ID;
oDataPrevious.I nsert();
}
catch
{
Status_Event(10 8,"Error : Could not update previous data") ;
}
try
{
oDataQueue.Dele te();
}
catch
{
Status_Event(10 1,"Error : Could not Delete DataQueue item") ;
}
}
else
{
try
{
oDataQueue.Fail ed();
Status_Event(10 2,"Error : Failed to process queue entry") ;
}
catch
{
Status_Event(10 3,"Error : Could not Mark DataQueue Failed") ;
}
}
}
else // error creating computernew. No exception AccountID set to 0
{
try
{
oDataQueue.Fail ed();
Status_Event(10 5,"Error : XML - Could not create computer new object")
;
}
catch
{
Status_Event(10 6,"Error : Could not Mark DataQueue Failed") ;
}
}
}
catch
{
Status_Event(10 4,"Error : Could not create Computer New Object") ;
}
}
}
catch (Exception ex)
{
Status_Event(10 0,"Error : Could not get next item in DataQueue") ;
Status_Event(10 0,"Exception : " + ex.Message ) ;
}
}
private void Status_Event(in t code , string message)
{
int codeoffset = 2000 ;
code += codeoffset ;
eQueueProcess_S tatus.StatusCod e = code ;
eQueueProcess_S tatus.StatusMes sage = message ;
QueueProcess_St atus_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_St atus( object sender,
QueueProcess_St atus_EventArgs e) ;
public class QueueProcess_St atus_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
}
}
[/code]
Comment