Service With Tread Don't Stop

Z

Zahi S

Hi,
Here is my service class, It simply don't stop when i remove the thread
everything is OK:

using System;
using System.Threading;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Xml;
using System.IO;

namespace MzDirMove
{
public class MzDirMove : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
ThreadStart otter;
Thread oThread;
private XmlDocument xDoc;
private bool tloop=true;


public MzDirMove()
{
// 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
MzDirMove() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// MzDirMove
//
this.CanHandlePowerEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.ServiceName = "MzDirMove";

}

/// <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)
{
EventLog.WriteEntry("MzDirMove","MzDirMove
starting",EventLogEntryType.Information,4000);
xDoc=new XmlDocument();
xDoc.Load("C:\\work\\mzDirMoveZ\\mzdirmove.xml");
otter = new ThreadStart(Route);
Thread oThread = new Thread(otter);
oThread.Start();
EventLog.WriteEntry("MzDirMove","MzDirMove starting
2",EventLogEntryType.Information,4000);

}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
tloop=false;
// Time should be longer than Run() loop takes to execute.
Thread.Sleep(10000);
// If the thread hasn't exited on its own by now,
// Kill it.
if (oThread.IsAlive) oThread.Abort();

oThread = (System.Threading.Thread) null;
}
public void Route()
{

while (tloop)
{
Thread.Sleep(1000);
// Do the rest here
}
}
}
}
 
J

Jon Skeet [C# MVP]

Zahi S said:
Here is my service class, It simply don't stop when i remove the thread
everything is OK:

<snip>

Please see my response to your previous post.
 

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