How to disable automatic stop on window service?

L

Lemune

Hi,

I'm developing window service application on C# 2005.
The service is to read a new excel file on certain directory, and
process it to database.
The service work find on XP.
But when I install the application on Windows Server 2003, when i
start the service it said:
"The <my service> on Local Computer started and then stop. Some
service stop automatically if they have no work to do , for example ,
the Performance Logs and Alerts System."

So my service will never run.

On my service I'm using File System Watcher Component.

What should I do?

Thanks.
 
P

Pavel Minaev

Hi,

I'm developing window service application on C# 2005.
The service is to read a new excel file on certain directory, and
process it to database.
The service work find on XP.
But when I install the application on Windows Server 2003, when i
start the service it said:
"The <my service> on Local Computer started and then stop. Some
service stop automatically if they have no work to do , for example ,
the Performance Logs and Alerts System."

So my service will never run.

On my service I'm using File System Watcher Component.

It would help if you explain in more details how exactly you're using
that component (and, in general, how your service is written). Code
snippets would be even better.
 
J

Jeroen Mostert

Lemune said:
I'm developing window service application on C# 2005.
The service is to read a new excel file on certain directory, and
process it to database.
The service work find on XP.
But when I install the application on Windows Server 2003, when i
start the service it said:
"The <my service> on Local Computer started and then stop. Some
service stop automatically if they have no work to do , for example ,
the Performance Logs and Alerts System."
This means your service crashed during startup, which could have any number
of causes. Consult the event log for more details, and hook up an event
handler to AppDomain.UnhandledException in your service that logs to a file.
 
B

Brian Gideon

This means your service crashed during startup, which could have any number
of causes. Consult the event log for more details, and hook up an event
handler to AppDomain.UnhandledException in your service that logs to a file.

I can't remember for certain, but it sounds more like the service
stopped by choice. When a service crashes or throws an exception you
get a message stating that the service failed to start altogether.
 
J

Jeroen Mostert

Brian said:
I can't remember for certain, but it sounds more like the service
stopped by choice. When a service crashes or throws an exception you
get a message stating that the service failed to start altogether.

Not on Windows XP, at least; I tested it. If the service throws an exception
in its OnStart(), you'll get the exact message the OP described. The event
log will contain the exception details.

A .NET service can't really stop by choice, except through calling
Environment.Exit() (or by explicitly connecting to the SCM and stopping
itself, but that's just backwards).
 
L

Lemune

Thanks all for your quict reply

this is my snipped code ( I just translate it because i use my on
language)


Code:
public partial class HCCPReader : ServiceBase
{
DateTime _lastFileTime = DateTime.MinValue;
protected DateTime _lastFTPTime = DateTime.MinValue;
protected DateTime _lastUploadTime = DateTime.MinValue;
protected DateTime _lastBankUploadTime = DateTime.MinValue;

System.Timers.Timer _newTimer = new System.Timers.Timer();

public HCCPReader()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
_lastFileTime = DateTime.MinValue;
fswUpload.Path = Common.GetUploadSettlementPath();
fswUpload.EnableRaisingEvents = true;
fswFtp.Path = Common.GetFTPPath();
fswFtp.EnableRaisingEvents = true;
fswBankNotify.Path = Common.GetUploadBankPath();
fswBankNotify.EnableRaisingEvents=true;
_newTimer.Interval =3600000;
_newTimer.Elapsed += new
System.Timers.ElapsedEventHandler(ServiceTimer_Tick);
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop
your service.
fswUpload.EnableRaisingEvents = false;
fswFtp.EnableRaisingEvents = false;
fswBankNotify.EnableRaisingEvents = false;
}

private void ServiceTimer_Tick(object sender,
System.Timers.ElapsedEventArgs e)
{
SunFile.Clear();
Clear.ClearDirectory();
}
private void fswFtp_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
if (_fswFileTime > _lastFTPTime)
{
_lastFTPTime = _fswFileTime;
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
int _tries = 0;
while (!_isFileComplete && _tries < 15)
{
System.Threading.Thread.Sleep(1000);
_tries++;
_isFileComplete = Common.IsFileCompete(e.FullPath);
}
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
//column 10
string[] _arrayFileName = e.Name.Split('_');
Decimal _fileTotal = 0;
Decimal _totalAmount = 0;
try
{
_fileTotal = Convert.ToDecimal(_arrayFileName[3]);
}
catch
{
_fileTotal = -1;
TReadingLog.Add(e.Name, "Wrong Template", "File name is not same
with file name template.");
}
if (_fileTotal != -1)
{
foreach (DataRow _record in _dataExcel.Rows)
{
if (_record[0].ToString() == "S" || _record[0].ToString() == "Z" ||
_record[0].ToString() == "T" || _record[0].ToString() == "P")
{
_totalAmount += Convert.ToDecimal(_record[11]);
}
}
if (_fileTotal != _totalAmount)
{
TReadingLog.Add(e.Name, "Diference Amount", "Total on FileName (" +
_fileTotal.ToString() +
") is diffrent with file content (" + _totalAmount.ToString() +
".");
}
else
{
Status _insertMessage = new Status();
if (Common.IsInActiveDate(_arrayFileName[2]))
{
_insertMessage = Settlement.Inserts(_dataExcel);
}
else
{
_insertMessage = Settlement.Inserts(_dataExcel, true);
}
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data settlement has been
inserted. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data settlement failed to be
inserted. " + _insertMessage.Remark);
}
}
}
}
else
{
_lastFTPTime = DateTime.MinValue;
}

}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswFtp_Changed", _error.Message);
}
finally
{
_dataExcel.Dispose();
}
}

private void fswUpload_Changed(object sender, FileSystemEventArgs e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
if (_fswFileTime > _lastFTPTime)
{
_lastFTPTime = _fswFileTime;
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
int _tries = 0;
while (!_isFileComplete && _tries < 15)
{
System.Threading.Thread.Sleep(1000);
_tries++;
_isFileComplete = Common.IsFileCompete(e.FullPath);
}
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
//column 10
string[] _arrayFileName = e.Name.Split('_');
Decimal _fileTotal = 0;
Decimal _totalAmount = 0;
try
{
_fileTotal = Convert.ToDecimal(_arrayFileName[3]);
}
catch
{
_fileTotal = -1;
TReadingLog.Add(e.Name, "Wrong Template", "File name is not same
with file name template.");
}
if (_fileTotal != -1)
{
foreach (DataRow _record in _dataExcel.Rows)
{
if (_record[0].ToString() == "S" || _record[0].ToString() == "Z" ||
_record[0].ToString() == "T" || _record[0].ToString() == "P")
{
_totalAmount += Convert.ToDecimal(_record[11]);
}
}
if (_fileTotal != _totalAmount)
{
TReadingLog.Add(e.Name, "Diference Amount", "Total on FileName (" +
_fileTotal.ToString() +
") is diffrent with file content (" + _totalAmount.ToString() +
".");
}
else
{
Status _insertMessage = new Status();
if (Common.IsInActiveDate(_arrayFileName[2]))
{
_insertMessage = Settlement.Inserts(_dataExcel);
}
else
{
_insertMessage = Settlement.Inserts(_dataExcel, true);
}
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data settlement has been
inserted. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data settlement failed to be
inserted. " + _insertMessage.Remark);
}
}
}
}
else
{
_lastFTPTime = DateTime.MinValue;
}

}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswUpload_Changed", _error.Message);
}
finally
{
_dataExcel.Dispose();
}
}

private void fswFtp_Created(object sender, FileSystemEventArgs e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
string[] _arrayFileName = e.Name.Split('_');
Decimal _fileTotal = 0;
Decimal _totalAmount = 0;
try
{
_fileTotal = Convert.ToDecimal(_arrayFileName[3]);
}
catch
{
_fileTotal = -1;
TReadingLog.Add(e.Name, "Wrong Template", "File name is not same
with file name template.");
}
if (_fileTotal != -1)
{
foreach (DataRow _record in _dataExcel.Rows)
{
if (_record[0].ToString() == "S" || _record[0].ToString() == "Z"
|| _record[0].ToString() == "T" || _record[0].ToString() == "P")
{
_totalAmount += Convert.ToDecimal(_record[11]);
}
}
if (_fileTotal != _totalAmount)
{
TReadingLog.Add(e.Name, "Diference Amount", "Total on FileName
(" + _fileTotal.ToString() +
") is diffrent with file content (" + _totalAmount.ToString() +
".");
}
else
{
Status _insertMessage = new Status();
if (Common.IsInActiveDate(_arrayFileName[2]))
{
_insertMessage = Settlement.Inserts(_dataExcel);
}
else
{
_insertMessage = Settlement.Inserts(_dataExcel, true);
}
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data settlement has been
inserted. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data settlement failed to be
inserted. " + _insertMessage.Remark);
}
}
}
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswFtp_Created", _error.Message);
}
finally
{
_dataExcel.Dispose();
}
}

private void fswUpload_Created(object sender, FileSystemEventArgs e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
string[] _arrayFileName = e.Name.Split('_');
Decimal _fileTotal = 0;
Decimal _totalAmount = 0;
try
{
_fileTotal = Convert.ToDecimal(_arrayFileName[3]);
}
catch
{
_fileTotal = -1;
TReadingLog.Add(e.Name, "Wrong Template", "File name is not same
with file name template.");
}
if (_fileTotal != -1)
{
foreach (DataRow _record in _dataExcel.Rows)
{
if (_record[0].ToString() == "S" || _record[0].ToString() == "Z"
|| _record[0].ToString() == "T" || _record[0].ToString() == "P")
{
_totalAmount += Convert.ToDecimal(_record[11]);
}
}
if (_fileTotal != _totalAmount)
{
TReadingLog.Add(e.Name, "Diference Amount", "Total on FileName
(" + _fileTotal.ToString() +
") is diffrent with file content (" + _totalAmount.ToString() +
".");
}
else
{

Status _insertMessage = new Status();
if(Common.IsInActiveDate(_arrayFileName[2]))
{
_insertMessage=Settlement.Inserts(_dataExcel);
}
else
{
_insertMessage=Settlement.Inserts(_dataExcel,true);
}
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name,"Success", "Data settlement has been
inserted. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name,"Failed", "Data settlement failed to be
inserted. " + _insertMessage.Remark);
}
}
}
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswUpload_Created", _error.Message);
}
finally
{
_dataExcel.Dispose();
}
}

private void fswBankNotify_Changed(object sender, FileSystemEventArgs
e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
if (_fswFileTime > _lastBankUploadTime)
{
_lastBankUploadTime = _fswFileTime;
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
int _tries = 0;
while (!_isFileComplete && _tries < 15)
{
System.Threading.Thread.Sleep(1000);
_tries++;
_isFileComplete = Common.IsFileCompete(e.FullPath);
}
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
Status _insertMessage = new Status();
_insertMessage = BankNotification.Process(_dataExcel);
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data bank notification
berhasil diinput. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data bank notification gagal
diinput. " + _insertMessage.Remark);
}
FileInfo _fileInfo = new FileInfo(e.FullPath);
TTempBankNotification.UpdateStatus(_fileInfo.DirectoryName,
_insertMessage);
File.Delete(e.FullPath);
}
else
{
_lastFTPTime = DateTime.MinValue;
}
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswBankNotify_Changed",
_error.Message);
}
finally
{
_dataExcel.Dispose();
}
}

private void fswBankNotify_Created(object sender, FileSystemEventArgs
e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
Status _insertMessage = new Status();
_insertMessage = BankNotification.Process(_dataExcel);
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data bank notification berhasil
diinput. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data bank notification gagal
diinput. " + _insertMessage.Remark);
}
FileInfo _fileInfo= new FileInfo(e.FullPath);
TTempBankNotification.UpdateStatus(_fileInfo.DirectoryName,
_insertMessage);
File.Delete(e.FullPath);
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswBankNotify_Created",
_error.Message);
}
finally
{
_dataExcel.Dispose();
}
}
}

My code just run well on MS XP, but it has this problem on MS Server
2003
 
B

Brian Gideon

Not on Windows XP, at least; I tested it. If the service throws an exception
in its OnStart(), you'll get the exact message the OP described. The event
log will contain the exception details.

A .NET service can't really stop by choice, except through calling
Environment.Exit() (or by explicitly connecting to the SCM and stopping
itself, but that's just backwards).

Yep, I tested it too. You are correct. I guess my memory isn't that
good :)
 
L

Lemune

Thanks Jeroen,

I check event log, and I have found my error.
It's just because the my exe config that has some error.
I have fixed my problem.

Thanks all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top