自己写服务的话,有线程的启动与停止,所以写了简单的实现的代码。
代码
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("文件服务输出!");
            }
        }
    }
}

 

相关文章:

  • 2021-07-15
  • 2021-11-14
  • 2021-08-25
  • 2021-08-10
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-10
  • 2021-10-08
  • 2021-12-22
  • 2021-04-05
  • 2022-12-23
相关资源
相似解决方案