自己写服务的话,有线程的启动与停止,所以写了简单的实现的代码。

using System;
using System.Collections.Generic;
using System.Text;
namespace Thread.Stop
{
/// <summary>
/// 文件服务
/// </summary>
public class FileService
{
private volatile bool isStop;
private System.Threading.Thread fileThread = null;
public FileService()
{ }
/// <summary>
/// 启动服务
/// </summary>
public void Start()
{
fileThread = new System.Threading.Thread(new System.Threading.ThreadStart(TranslateFile));
fileThread.Start();
}
/// <summary>
/// 中止服务
/// </summary>
public void Stop()
{
isStop = true;
if (fileThread != null && fileThread.ThreadState == System.Threading.ThreadState.Running)
fileThread.Abort();
fileThread = null;
}
private void TranslateFile()
{
while (!isStop)
{
//模拟长时间操作
Console.WriteLine("文件服务输出!");
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Thread.Stop
{
/// <summary>
/// 文件服务
/// </summary>
public class FileService
{
private volatile bool isStop;
private System.Threading.Thread fileThread = null;
public FileService()
{ }
/// <summary>
/// 启动服务
/// </summary>
public void Start()
{
fileThread = new System.Threading.Thread(new System.Threading.ThreadStart(TranslateFile));
fileThread.Start();
}
/// <summary>
/// 中止服务
/// </summary>
public void Stop()
{
isStop = true;
if (fileThread != null && fileThread.ThreadState == System.Threading.ThreadState.Running)
fileThread.Abort();
fileThread = null;
}
private void TranslateFile()
{
while (!isStop)
{
//模拟长时间操作
Console.WriteLine("文件服务输出!");
}
}
}
}