473,503 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows Service Hangs

Hello All,

We have around 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. Any help would be appreciated.

using System;
using System.IO ;
using System.Threading ;
using System.Xml ;
using Csc.Lisa.Dal ;
using Csc.Lisa.Dal.Entities ;
using Csc.Lisa.Dal.DpsEntities ;

namespace Csc.Lisa.Dls.Queue
{
/// <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_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 ;
/// <summary>
/// Begin polling the folders in the path_incoming for ZIP files
///
/// </summary>
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

/// <summary>
/// Method to Start processing QueueProcess
/// </summary>
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("<lisa/>") ;
}
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("<lisa/>") ;
}

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_Statu s) ;
}

#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

}
}

Oct 22 '08 #1
3 1705

"mohithmohith" wrote:
Hello All,

We have around 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. Any help would be appreciated.
When this happens, it is usually due to a timer issue, especially if you use
a System.Windows.Forms.Timer which should never be used in a Windows Service.
If you run continuously you may also end up using all the ticks (42 day
cycle I believe) which will cause events that should trigger at a specific
hour to suddenly be misalligned. The solution for the first case is to use a
System.Threading.Timer or System.Timers.Timer, the second case is to recreate
and readjust the timer/interval at each tick.

--
Happy Coding!
Morten Wennevik [C# MVP]

Oct 22 '08 #2
Thanks a lot for your ideas Morten...
Morten Wennevik [C# MVP] wrote:
>Hello All,
[quoted text clipped - 7 lines]
>files. But when the processes are restarted, they run just fine until any one
hangs again. Any help would be appreciated.

When this happens, it is usually due to a timer issue, especially if you use
a System.Windows.Forms.Timer which should never be used in a Windows Service.
If you run continuously you may also end up using all the ticks (42 day
cycle I believe) which will cause events that should trigger at a specific
hour to suddenly be misalligned. The solution for the first case is to use a
System.Threading.Timer or System.Timers.Timer, the second case is to recreate
and readjust the timer/interval at each tick.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200810/1

Oct 23 '08 #3
Hi Morten,

I am not using timers for this program..,I still do not know what is causing
this problem..

Regards,
Mohith

Morten Wennevik [C# MVP] wrote:
>Hello All,
[quoted text clipped - 7 lines]
>files. But when the processes are restarted, they run just fine until any one
hangs again. Any help would be appreciated.

When this happens, it is usually due to a timer issue, especially if you use
a System.Windows.Forms.Timer which should never be used in a Windows Service.
If you run continuously you may also end up using all the ticks (42 day
cycle I believe) which will cause events that should trigger at a specific
hour to suddenly be misalligned. The solution for the first case is to use a
System.Threading.Timer or System.Timers.Timer, the second case is to recreate
and readjust the timer/interval at each tick.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200810/1

Oct 24 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4374
by: Jacob Crossley | last post by:
Hello all. We have about 10 Window's services that we wrote in c#. We use them to process row's that we have queued up in various SQL tables. The services seem to hang at least once in any given...
4
8995
by: Kris | last post by:
I have a Windows Service in C# talking to a serial port and using Remoting. It also uses several COM objects. On customer's computer the service will occassionally hang somewhere - the service...
5
2389
by: Andrea Vincenzi | last post by:
Help me please, I'm totally stuck! My Visual Studio 2003 debugger stopped working after I installed Windows XP Service Pack 2. Here is what happens (with any project, even a "Hello, world"...
4
8789
by: Steven De Smet | last post by:
Hello, This is my first post. I searched on the internet for answers but I was unable to solve my problem. So I hope that you guy's can help me with my VB.NET problem I tried to create a...
7
3192
by: Ashish Khandelwal | last post by:
I have a Windows Service in C# talking to a serial port and using Remoting. It also uses several COM objects. On server the service will occassionally hang somewhere - the service still shows on a...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.