Answers

Nov 02, 2009 - 11:41 AM
Don't do it in the OnStart, instead in the contstructor or a method called by the constructor, create a thread that does the work. The thread would then initiate the connection or start the listener. The service control manager expects OnStart to return right away and not delay.
example:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.ServiceModel;
using System.ServiceProcess;
using System.Threading;
using System.IO;
namespace FileService
{
public class FileProcessService : ServiceBase
{
const string BASE_THREAD_NAME = "FileService";
private EventWaitHandle _stopThread = null;
private string _runningThreadName;
private ServiceHost _serviceHost = null;
public FileProcessService()
{
Initialize();
}
protected virtual void Initialize()
{
if (_stopThread == null) _stopThread = new ManualResetEvent(false);
else _stopThread.Reset();
Thread t = new Thread(new ThreadStart(StartService));
t.Name = BASE_THREAD_NAME;
_runningThreadName = t.Name;
t.Start();
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
}
protected override void OnStop()
{
base.OnStop();
StopService();
_serviceHost.Close();
}
protected override void OnShutdown()
{
base.OnShutdown();
}
protected void StartService()
{
_serviceHost = new ServiceHost(typeof(FileLoggingService));
_serviceHost.Open();
while (!_stopThread.WaitOne(500, true))
{
FileProcess.Instance.ProcessFiles(); //This class would be a singleton class responsible for processing the files.
if (_serviceHost.State == CommunicationState.Faulted)
{
_serviceHost.Abort();
_serviceHost = null;
_serviceHost = new ServiceHost(typeof(FileLoggingService));
_serviceHost.Open();
}
}
_serviceHost.Close();
}
public void StopService()
{
_stopThread.Set();
FileProcess.Instance.StopProcessing();
}
private void InitializeComponent()
{
this.ServiceName = "FileProcessor";
}
}
}
example:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.ServiceModel;
using System.ServiceProcess;
using System.Threading;
using System.IO;
namespace FileService
{
public class FileProcessService : ServiceBase
{
const string BASE_THREAD_NAME = "FileService";
private EventWaitHandle _stopThread = null;
private string _runningThreadName;
private ServiceHost _serviceHost = null;
public FileProcessService()
{
Initialize();
}
protected virtual void Initialize()
{
if (_stopThread == null) _stopThread = new ManualResetEvent(false);
else _stopThread.Reset();
Thread t = new Thread(new ThreadStart(StartService));
t.Name = BASE_THREAD_NAME;
_runningThreadName = t.Name;
t.Start();
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
}
protected override void OnStop()
{
base.OnStop();
StopService();
_serviceHost.Close();
}
protected override void OnShutdown()
{
base.OnShutdown();
}
protected void StartService()
{
_serviceHost = new ServiceHost(typeof(FileLoggingService));
_serviceHost.Open();
while (!_stopThread.WaitOne(500, true))
{
FileProcess.Instance.ProcessFiles(); //This class would be a singleton class responsible for processing the files.
if (_serviceHost.State == CommunicationState.Faulted)
{
_serviceHost.Abort();
_serviceHost = null;
_serviceHost = new ServiceHost(typeof(FileLoggingService));
_serviceHost.Open();
}
}
_serviceHost.Close();
}
public void StopService()
{
_stopThread.Set();
FileProcess.Instance.StopProcessing();
}
private void InitializeComponent()
{
this.ServiceName = "FileProcessor";
}
}
}

Dec 15, 2009 - 06:41 AM
The question looks to be abandoned by the user who asked it. If no action is taken within 2 days, a Quomon Moderator will consider closing the question and distributing the points.
The Quomon Team
The Quomon Team
Add New Comment