Answers
Nov 02, 2009 - 11:28 AM
Here is a stripped down service template I have. The actual file processing would be handle by the FileProcess class, I haven't included it due to the fact it would require a lot changes for customization to what you may need.
Hope this helps.
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;
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();
}
protected override void OnShutdown()
{
base.OnShutdown();
}
protected void StartService()
{
while (!_stopThread.WaitOne(500, true))
{
FileProcess.Instance.ProcessFiles(); //This class would be a singleton class responsible for processing the files.
}
}
public void StopService()
{
_stopThread.Set();
FileProcess.Instance.StopProcessing();
}
private void InitializeComponent()
{
this.ServiceName = "FileProcessor";
}
}
}
Nov 02, 2009 - 11:33 AM
FYI, the FileProcessor class would be responsible for iterating the directory/directories. It also would read the config file to get the directory/directories that need to be processed.
Dec 15, 2009 - 06:41 AM
The Quomon Team
Add New Comment