If you write services by yourself, there are threads to start and stop, so you write simple implementation code. Code
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Thread. Stop
{
/// <Summary>
/// File service
/// </Summary>
Public class FileService
{
Private volatile bool isStop;
Private System. Threading. Thread fileThread = null;
Public FileService ()
{}
/// <Summary>
/// Start the service
/// </Summary>
Public void Start ()
{
FileThread = new System. Threading. Thread (new System. Threading. ThreadStart (TranslateFile ));
FileThread. Start ();
}
/// <Summary>
/// Stop the service
/// </Summary>
Public void Stop ()
{
IsStop = true;
If (fileThread! = Null & fileThread. ThreadState = System. Threading. ThreadState. Running)
FileThread. Abort ();
FileThread = null;
}
Private void TranslateFile ()
{
While (! IsStop)
{
// Simulate a long operation
Console. WriteLine ("file service output! ");
}
}
}
}