Windows服务挂起 [英] windows Service Hangs

查看:88
本文介绍了Windows服务挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我们在c#中编写了大约10个Window的服务。我们使用

来处理我们在各种SQL表中排队的行'


这些服务似乎至少挂了一次24小时。一旦我们重新启动该服务,它将继续处理它的处理业务,直到下一次它挂起来为
。我们想知道挂起的问题是由于负载问题导致的吗?b $ b(单台机器上的Windows服务太多),代码缺陷还是固有的

服务从未挂起。我想知道我们是否要在旧学校vb6中重写所有的c#服务。我已将服务代码

包括在内......任何帮助都将不胜感激。


使用系统;

使用System.Collections;

使用System.ComponentModel;

使用System.Data;

使用System.Diagnostics;

使用System.ServiceProcess;

使用System.Configuration;


命名空间IAR_Service

{

公共类IAR:System.ServiceProcess.ServiceBase

{

private System.Timers.Timer tmrWinService;

///< summary>

///所需的设计变量。

///< / summary>

private System.ComponentModel.Container components = null;


public IAR()

{

// Windows.Forms组件设计师需要此调用。

InitializeComponent();


// TODO:在InitComponent调用后添加任何初始化

}


// t的主要入口点他处理

static void Main()

{

System.ServiceProcess.ServiceBase [] ServicesToRun;


//多个用户服务可以在同一进程中运行。要向此进程添加

//其他服务,请将以下行更改为

//创建第二个服务对象。例如,

//

// ServicesToRun = New System.ServiceProcess.ServiceBase [] {new

Service1(),new MySecondUserService( )};

//

ServicesToRun = new System.ServiceProcess.ServiceBase [] {new IAR()};


System.ServiceProcess.ServiceBase.Run(ServicesToRu n);

}


///< summary>

// / Designer支持所需的方法 - 不要使用代码编辑器修改

///此方法的内容。

///< / summary>

private void InitializeComponent()

{

this.tmrWinService = new System.Timers.Timer();


((System.ComponentModel.ISupportInitialize)(this.t mrWinService))。BeginInit()

;

//

// tmrWinService

//

//this.tmrWinService.Enabled = true;

this.tmrWinService.Interval = 20000;

this.tmrWinService.Elapsed + = new

System.Timers.ElapsedEv entHandler(this.tmrWinServi ce_Elapsed);

//

// IAR

//

this.AutoLog = false;

this.CanPauseAndContinue = true;

this.ServiceName =" IAR";


((系统。 ComponentModel.ISupportInitialize)(this.t mrWinService))。EndInit();


}


///< summary>

///清理正在使用的所有资源。

///< / summary>

protected override void Dispose(bool disposing)

{

if(处理)

{

if(components!= null)

{

components.Dispose();

}

}

base.Dispose(disposing); < br $>
}


///< summary>

///设置动态,以便您的服务可以完成其工作。

///< / summary>

protected override void OnStart(string [] args)

{

// TODO:在此处添加代码以启动您的服务。

tm rWinService.Enabled = true;

tmrWinService.AutoReset = true;

tmrWinService.Start();

//查看日志是否存在。如果没有,则创建

if(!EventLog.SourceExists(" IAR-Service"))EventLog.CreateEventSource("",

" IAR-Service" ,");

//日志

EventLog log = new EventLog();

log.Log =" IAR -Service" ;;

log.Source =" WinServiceIAR.OnStart";

log.WriteEntry(" Starting IAR windows Service",

EventLogEntryType.Information);

log.Dispose();

}


///< summary>

///停止此服务。

///< / summary>

protected override void OnStop()

{

// TODO:在这里添加代码以执行停止

服务所需的任何拆除。

tmrWinService.Enabled = true ;

tmrWinService.AutoReset = true;

tmrWinService.Stop();

//查看日志是否存在。如果没有,则创建

if(!EventLog.SourceExists(" IAR-Service"))EventLog.CreateEventSource("",

" IAR-Service" ,");

//日志

EventLog log = new EventLog();

log.Log =" IAR -Service" ;;

log.Source =" WinServiceIAR.OnStop";

log.WriteEntry(" Stopping IAR windows Service",

EventLogEntryType.Information);

log.Dispose();

}


private void tmrWinService_Elapsed(Object sender,

System.Timers.ElapsedEventArgs e)

{

this.tmrWinService.Stop();

try

{

UserServices oUser = new UserServices();

ScriptingTelnet st = new ScriptingTelnet();

// CALL

oUser.processIAR();


}

catch(异常错误)

{

//查看日志是否存在。如果没有,创建

if(!EventLog.SourceExists(" IAR-Service"))

EventLog.CreateEventSource(""," IAR-Service" ,");

//日志

EventLog log = new EventLog();

log.Log =" IAR -Service" ;;

log.Source =" WinServiceIAR.tmrWinService_Elapsed";

log.WriteEntry(err.Message.ToString(),EventLogEntryType.Error);

log.Dispose();

}

终于

{

this.tmrWinService .Start();

}

}

}

}

解决方案

这是什么目的?


ScriptingTelnet st = new ScriptingTelnet();


Willy。


" Jacob Crossley" < JA *********** @ hotmail.com>在消息中写道

新闻:WY *************** @ fe25.usenetserver.com ...

大家好。我们在c#中编写了大约10个Window的服务。我们使用它们来处理我们在各种SQL表中排队的行。

服务似乎在任何给定的24小时内至少挂起一次。一旦我们重新启动服务,它就会处理它的处理业务,直到下一个
它挂起。我们想知道悬挂问题是由于负载问题(单台机器上的Windows服务太多),代码缺陷还是固有的c#bug?我们曾经使用ntsvc.ocx在VB6中编写NT服务。那些服务永远不会挂起。我想知道我们是否要在旧学校vb6中重写所有
我们的c#服务。我已经在下面提供了服务代码
...非常感谢任何帮助。

使用System;
使用System.Collections;
使用System。 ComponentModel;
使用System.Diagnostics;
使用System.ServiceProcess;
使用System.Configuration;

命名空间IAR_Service
{
公共类IAR:System.ServiceProcess.ServiceBase
私有System.Timers.Timer tmrWinService;
///< summary>
// /必需的设计变量。
///< / summary>
私有System.ComponentModel.Container组件= null;

公共IAR()
{
// Windows.Forms组件设计器需要此调用。
InitializeComponent();

// TODO:在InitComponent调用后添加任何初始化
}

//进程的主要入口点
static void Main()
{/> System.ServiceProcess.ServiceBase [] ServicesToRun;

//多个用户服务可以在同一个进程中运行。要将
//其他服务添加到此流程,请将以下行更改为
//创建第二个服务对象。例如,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase [] {new
Service1(),new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase [] {new IAR()};

System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}

> ///< summary>
/// Designer支持所需的方法 - 不要使用代码编辑器修改
///此方法的内容。
///< / summary>
private void InitializeComponent()
{
this.tmrWinService = new System.Timers.Timer();

((System.ComponentModel.ISupportInitialize) (this.t mrWinService))。BeginInit()
;
//
// tmrWinService
//
//this.tmrWinService.Enabled = true;
this.tmrWinService.Interval = 20000;
this.tmrWinService.Elapsed + = new
System.Timers.ElapsedEventHandler(this.tmrWinServi ce_Elapsed);
//
/ / IAR
//
this.AutoLog = false;
this.CanPauseAndContinue = true;
this.ServiceName =" IAR";

((System.ComponentModel.ISupportInitialize)(this.t mrWinService ))。EndInit();

}
///< summary>
///清理正在使用的所有资源。
/ //< / summary>
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components!= null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
/// < summary>
///设置动态以便您的服务可以正常工作。
///< / summary>
protected override void OnStart(string [] args)
//
// TODO:在这里添加代码以启动服务。
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Start();
//查看日志是否存在。若否,则创建
if(!EventLog.SourceExists(" IAR-Service"))
EventLog.CreateEventSource("",
" IAR-Service","。 ");
// log
EventLog log = new EventLog();
log.Log =" IAR-Service";
log.Source =" WinServiceIAR。 OnStart" ;;
log.WriteEntry(" Starting IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}
///< summary>
///停止此服务。
///< / summary>
受保护的覆盖void OnStop()
{
// TODO:在这里添加代码以执行停止服务所需的任何拆卸。
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Stop ();
//查看日志是否存在。若否,则创建
if(!EventLog.SourceExists(" IAR-Service"))
EventLog.CreateEventSource("",
" IAR-Service","。 ");
// log
EventLog log = new EventLog();
log.Log =" IAR-Service";
log.Source =" WinServiceIAR。 OnStop" ;;
log.WriteEntry(" Stopping IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}
private void tmrWinService_Elapsed(Object sender,
System.Timers.ElapsedEventArgs e)
{
this.tmrWinService.Stop();
尝试
{
UserServices oUser = new UserServices();
ScriptingTelnet st = new ScriptingTelnet();
// CALL
oUser.processIAR();

}
catch(例外错误)
{
//查看日志是否存在。若否,则创建
if(!EventLog.SourceExists(" IAR-Service"))
EventLog.CreateEventSource(""," IAR-Service","。");
// log
EventLog log = new EventLog();
log.Log =" IAR-Service";
log.Source =" WinServiceIAR.tmrWinService_Elapsed"; < br。> log.WriteEntry(err.Message.ToString(),EventLogEntryType.Error);
log.Dispose();
}
最后
{
这个.tmrWinService.Start();
}
}
}



这只是实例化了sln中的另一个类....它是一个自定义类,

添加并删除路由器上的acl来限制/允许互联网

访问我们的ISP ......如果你愿意,我可以包含那些代码,但我们所有的

服务都会挂起,无论他们在工人阶级做什么......我都会/>
建议即使它调用该服务也会挂起一个写了

''hello world''到一个文本文件的类。

Willy Denoyette [MVP]" <无线************* @ pandora.be>在消息中写道

新闻:uv ************** @ TK2MSFTNGP09.phx.gbl ...

什么是这个目的是什么?

ScriptingTelnet st = new ScriptingTelnet();

Willy。

Jacob Crossley < JA *********** @ hotmail.com>在消息中写道
新闻:WY *************** @ fe25.usenetserver.com ...

大家好。我们在c#中编写了大约10个Window的服务。我们
使用它们来处理我们在各种SQL表中排队的行。

服务似乎在任何给定的24小时内至少挂起一次。
一旦我们重新启动服务,它将继续处理它的处理业务,直到下一个
时间挂起。我们想知道悬挂问题是由于负载问题(单台机器上的Windows服务太多),代码缺陷还是固有的c#bug?我们曾经使用ntsvc.ocx在VB6中编写NT服务。那些服务永远不会挂起。我想知道我们是否要在旧学校vb6中重写
所有的c#服务。我已经在下面提供了服务代码
...非常感谢任何帮助。

使用System;
使用System.Collections;
使用System。 ComponentModel;
使用System.Diagnostics;
使用System.ServiceProcess;
使用System.Configuration;

命名空间IAR_Service
{
公共类IAR:System.ServiceProcess.ServiceBase
私有System.Timers.Timer tmrWinService;
///< summary>
// /必需的设计变量。
///< / summary>
私有System.ComponentModel.Container组件= null;

公共IAR()
{
// Windows.Forms组件设计器需要此调用。
InitializeComponent();

// TODO:在InitComponent调用后添加任何初始化
}

//进程的主要入口点
static void Main()
{/> System.ServiceProcess.ServiceBase [] ServicesToRun;

//多个用户服务可以在同一个进程中运行。要将
//其他服务添加到此流程,请将以下行更改为
//创建第二个服务对象。例如,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase [] {new
Service1(),new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase [] {new IAR()};

System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}

> ///< summary>
/// Designer支持所需的方法 - 不要使用代码编辑器修改
///此方法的内容。
///< / summary>
private void InitializeComponent()
{
this.tmrWinService = new System.Timers.Timer();


((系统。 ComponentModel.ISupportInitialize)(this.t mrWinService))。BeginInit();
//
// tmrWinService
//this.tmrWinService.Enabled = true;
this.tmrWinService.Interval = 20000;
this.tmrWinService.Elapsed + = new
System.Timers.ElapsedEventHandler(this.tmrWinServi ce_Elapsed);
//
/ / IAR
//
t his.AutoLog = false;
this.CanPauseAndContinue = true;
this.ServiceName =" IAR";


((System.ComponentModel.ISupportInitialize)( this.t mrWinService))。EndInit();
}
///< summary>
///清理正在使用的所有资源。
/ //< / summary>
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components!= null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
/// < summary>
///设置动态以便您的服务可以正常工作。
///< / summary>
protected override void OnStart(string [] args)
//
// TODO:在这里添加代码以启动服务。
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Start();
//查看日志是否存在。若否,则创建
if(!EventLog.SourceExists(" IAR-Service"))
EventLog.CreateEventSource("",
" IAR-Service","。 ");
// log
EventLog log = new EventLog();
log.Log =" IAR-Service";
log.Source =" WinServiceIAR。 OnStart" ;;
log.WriteEntry(" Starting IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}
///< summary>
///停止此服务。
///< / summary>
受保护的覆盖void OnStop()
{
// TODO:在这里添加代码以执行停止服务所需的任何拆卸。
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Stop ();
//查看日志是否存在。若否,则创建
if(!EventLog.SourceExists(" IAR-Service"))
EventLog.CreateEventSource("",
" IAR-Service","。 ");
// log
EventLog log = new EventLog();
log.Log =" IAR-Service";
log.Source =" WinServiceIAR。 OnStop" ;;
log.WriteEntry(" Stopping IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}
private void tmrWinService_Elapsed(Object sender,
System.Timers.ElapsedEventArgs e)
{
this.tmrWinService.Stop();
尝试
{
UserServices oUser = new UserServices();
ScriptingTelnet st = new ScriptingTelnet();
// CALL
oUser.processIAR();

}
catch(例外错误)
{
//查看日志是否存在。若否,则创建
if(!EventLog.SourceExists(" IAR-Service"))
EventLog.CreateEventSource(""," IAR-Service","。");
// log
EventLog log = new EventLog();
log.Log =" IAR-Service";
log.Source =" WinServiceIAR.tmrWinService_Elapsed"; < br。> log.WriteEntry(err.Message.ToString(),EventLogEntryType.Error);
log.Dispose();
}
最后
{
这个.tmrWinService.Start();
}
}
}






Willy。


" Jacob Crossley" < JA *********** @ hotmail.com>在消息中写道

新闻:il *************** @ fe25.usenetserver.com ...

这只是实例化另一个sln中的一个类......它是一个自定义类,它可以在路由器上添加和删除人员以限制/允许在我们的ISP上访问互联网...我可以包括那个代码,如果你愿意,但所有
我们的
服务都会挂起,不管他们在工人班里做什么......我会建议服务挂起即使它把一个写成hello world的类称为文本文件。




无需用简单的hello来检查世界"服务:-),我有一个

的服务,现在运行几周,使用System.Threading.Timer以常规

的间隔执行一些任务。


这个自定义类可能需要比间隔更长的时间吗?并且,

当它存在的可能性时,你确定,你的自定义类是

线程安全,并且没有可能导致竞争条件

死锁?

我建议您观看一些资源,如句柄数量,可运行线程数量

服务运行时以及何时挂起。

你可以做的另一件事是将一个调试器附加到你挂起的服务上并尝试

找出它挂起的位置。


威利。


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 24 hour period. Once we
reset the service, it goes about its processing business until the next time
it hangs. We are wondering if the hanging problem is due to a load issue
(too many windows services on a single machine), a code flaw, or an inherent
c# bug? We used to write NT services in VB6 using the ntsvc.ocx. Those
services never hung. I''m wondering if we are going to have to rewrite all of
our c# services in old school vb6. I''ve included the Service code
below...any help would be much appreciated.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration;

namespace IAR_Service
{
public class IAR : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer tmrWinService;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public IAR()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new IAR() };

System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tmrWinService = new System.Timers.Timer();

((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).BeginInit()
;
//
// tmrWinService
//
//this.tmrWinService.Enabled = true;
this.tmrWinService.Interval = 20000;
this.tmrWinService.Elapsed += new
System.Timers.ElapsedEventHandler(this.tmrWinServi ce_Elapsed);
//
// IAR
//
this.AutoLog = false;
this.CanPauseAndContinue = true;
this.ServiceName = "IAR";

((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).EndInit();

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Start();
//see if log exists. if not, create
if (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("",
"IAR-Service", ".");
//log
EventLog log = new EventLog();
log.Log = "IAR-Service";
log.Source = "WinServiceIAR.OnStart";
log.WriteEntry("Starting IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Stop();
//see if log exists. if not, create
if (!EventLog.SourceExists("IAR-Service")) EventLog.CreateEventSource("",
"IAR-Service", ".");
//log
EventLog log = new EventLog();
log.Log = "IAR-Service";
log.Source = "WinServiceIAR.OnStop";
log.WriteEntry("Stopping IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}

private void tmrWinService_Elapsed(Object sender,
System.Timers.ElapsedEventArgs e)
{
this.tmrWinService.Stop();
try
{
UserServices oUser = new UserServices();
ScriptingTelnet st = new ScriptingTelnet();
//CALL
oUser.processIAR();

}
catch (Exception err)
{
//see if log exists. if not, create
if (!EventLog.SourceExists("IAR-Service"))
EventLog.CreateEventSource("", "IAR-Service", ".");
//log
EventLog log = new EventLog();
log.Log = "IAR-Service";
log.Source = "WinServiceIAR.tmrWinService_Elapsed";
log.WriteEntry(err.Message.ToString(), EventLogEntryType.Error);
log.Dispose();
}
finally
{
this.tmrWinService.Start();
}
}
}
}

解决方案

What''s the purpose of this?

ScriptingTelnet st = new ScriptingTelnet();

Willy.

"Jacob Crossley" <ja***********@hotmail.com> wrote in message
news:WY***************@fe25.usenetserver.com...

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 24 hour period. Once
we
reset the service, it goes about its processing business until the next
time
it hangs. We are wondering if the hanging problem is due to a load issue
(too many windows services on a single machine), a code flaw, or an
inherent
c# bug? We used to write NT services in VB6 using the ntsvc.ocx. Those
services never hung. I''m wondering if we are going to have to rewrite all
of
our c# services in old school vb6. I''ve included the Service code
below...any help would be much appreciated.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration;

namespace IAR_Service
{
public class IAR : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer tmrWinService;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public IAR()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new IAR() };

System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tmrWinService = new System.Timers.Timer();

((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).BeginInit()
;
//
// tmrWinService
//
//this.tmrWinService.Enabled = true;
this.tmrWinService.Interval = 20000;
this.tmrWinService.Elapsed += new
System.Timers.ElapsedEventHandler(this.tmrWinServi ce_Elapsed);
//
// IAR
//
this.AutoLog = false;
this.CanPauseAndContinue = true;
this.ServiceName = "IAR";

((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).EndInit();

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Start();
//see if log exists. if not, create
if (!EventLog.SourceExists("IAR-Service"))
EventLog.CreateEventSource("",
"IAR-Service", ".");
//log
EventLog log = new EventLog();
log.Log = "IAR-Service";
log.Source = "WinServiceIAR.OnStart";
log.WriteEntry("Starting IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Stop();
//see if log exists. if not, create
if (!EventLog.SourceExists("IAR-Service"))
EventLog.CreateEventSource("",
"IAR-Service", ".");
//log
EventLog log = new EventLog();
log.Log = "IAR-Service";
log.Source = "WinServiceIAR.OnStop";
log.WriteEntry("Stopping IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}

private void tmrWinService_Elapsed(Object sender,
System.Timers.ElapsedEventArgs e)
{
this.tmrWinService.Stop();
try
{
UserServices oUser = new UserServices();
ScriptingTelnet st = new ScriptingTelnet();
//CALL
oUser.processIAR();

}
catch (Exception err)
{
//see if log exists. if not, create
if (!EventLog.SourceExists("IAR-Service"))
EventLog.CreateEventSource("", "IAR-Service", ".");
//log
EventLog log = new EventLog();
log.Log = "IAR-Service";
log.Source = "WinServiceIAR.tmrWinService_Elapsed";
log.WriteEntry(err.Message.ToString(), EventLogEntryType.Error);
log.Dispose();
}
finally
{
this.tmrWinService.Start();
}
}
}
}



That just instantiates another class in the sln....Its a custom class that
adds and removes people from an acl on a router to restrict/permit internet
access at our ISP...I could include that code if you''d like, but all of our
services hang, regardless of what they do in the worker classes...I''d
propose that the service would hang even if it called a class that wrote
''hello world'' to a text file.
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:uv**************@TK2MSFTNGP09.phx.gbl...

What''s the purpose of this?

ScriptingTelnet st = new ScriptingTelnet();

Willy.

"Jacob Crossley" <ja***********@hotmail.com> wrote in message
news:WY***************@fe25.usenetserver.com...

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 24 hour period. Once we
reset the service, it goes about its processing business until the next
time
it hangs. We are wondering if the hanging problem is due to a load issue
(too many windows services on a single machine), a code flaw, or an
inherent
c# bug? We used to write NT services in VB6 using the ntsvc.ocx. Those
services never hung. I''m wondering if we are going to have to rewrite all of
our c# services in old school vb6. I''ve included the Service code
below...any help would be much appreciated.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration;

namespace IAR_Service
{
public class IAR : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer tmrWinService;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public IAR()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new IAR() };

System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tmrWinService = new System.Timers.Timer();

((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).BeginInit() ;
//
// tmrWinService
//
//this.tmrWinService.Enabled = true;
this.tmrWinService.Interval = 20000;
this.tmrWinService.Elapsed += new
System.Timers.ElapsedEventHandler(this.tmrWinServi ce_Elapsed);
//
// IAR
//
this.AutoLog = false;
this.CanPauseAndContinue = true;
this.ServiceName = "IAR";

((System.ComponentModel.ISupportInitialize)(this.t mrWinService)).EndInit();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Start();
//see if log exists. if not, create
if (!EventLog.SourceExists("IAR-Service"))
EventLog.CreateEventSource("",
"IAR-Service", ".");
//log
EventLog log = new EventLog();
log.Log = "IAR-Service";
log.Source = "WinServiceIAR.OnStart";
log.WriteEntry("Starting IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
tmrWinService.Enabled = true;
tmrWinService.AutoReset = true;
tmrWinService.Stop();
//see if log exists. if not, create
if (!EventLog.SourceExists("IAR-Service"))
EventLog.CreateEventSource("",
"IAR-Service", ".");
//log
EventLog log = new EventLog();
log.Log = "IAR-Service";
log.Source = "WinServiceIAR.OnStop";
log.WriteEntry("Stopping IAR windows Service",
EventLogEntryType.Information);
log.Dispose();
}

private void tmrWinService_Elapsed(Object sender,
System.Timers.ElapsedEventArgs e)
{
this.tmrWinService.Stop();
try
{
UserServices oUser = new UserServices();
ScriptingTelnet st = new ScriptingTelnet();
//CALL
oUser.processIAR();

}
catch (Exception err)
{
//see if log exists. if not, create
if (!EventLog.SourceExists("IAR-Service"))
EventLog.CreateEventSource("", "IAR-Service", ".");
//log
EventLog log = new EventLog();
log.Log = "IAR-Service";
log.Source = "WinServiceIAR.tmrWinService_Elapsed";
log.WriteEntry(err.Message.ToString(), EventLogEntryType.Error);
log.Dispose();
}
finally
{
this.tmrWinService.Start();
}
}
}
}





Willy.

"Jacob Crossley" <ja***********@hotmail.com> wrote in message
news:il***************@fe25.usenetserver.com...

That just instantiates another class in the sln....Its a custom class that
adds and removes people from an acl on a router to restrict/permit
internet
access at our ISP...I could include that code if you''d like, but all of
our
services hang, regardless of what they do in the worker classes...I''d
propose that the service would hang even if it called a class that wrote
''hello world'' to a text file.



No need to check this with a simple "hello world" service :-),I have a
couple of services ,running for weeks now, performing some tasks at regular
intervals using System.Threading.Timer.

Is it possible that this custom class takes longer than the interval? And,
when it''s the possibility exists, are you sure that, your custom class is
threadsafe, and there is no possibility for race conditions leading to
deadlocks?
I suggest you watch some resources like # of handles, # of run-able threads
when the service runs and when it hangs.
Another thing you can do is attach a debugger to your hung service and try
to find out where it hangs.

Willy.


这篇关于Windows服务挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆