Skip to content

Instantly share code, notes, and snippets.

@blogmastigado
Last active September 26, 2018 17:46
Show Gist options
  • Save blogmastigado/142dae384a044a491ab4a3e2cbf810f8 to your computer and use it in GitHub Desktop.
Save blogmastigado/142dae384a044a491ab4a3e2cbf810f8 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
private static Thread thServico = new Thread(Servico);
public Service1()
{
InitializeComponent();
}
private static void Servico()
{
for(;;)
{
GravarTextoEmArquivo("SERVIÇO SENDO EXECUTADO AS " + DateTime.Now.ToString() + ".");
// Coloca a Thread para dormir por uma hora
Thread.Sleep(3600000); //1 hora
}
}
protected override void OnStart(string[] args)
{
try
{
AbrirArquivo("SERVIÇO INICIALIZADO AS " + DateTime.Now.ToString() + ".");
thServico.Start();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
protected override void OnStop()
{
try
{
AbrirArquivo("SERVIÇO PARALISADO AS " + DateTime.Now.ToString() + ".");
thServico.Abort();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
public static void GravarTextoEmArquivo(string texto)
{
StreamWriter vWriter = new StreamWriter(@"C:\Log\LogServico.log", true);
vWriter.WriteLine(texto);
vWriter.Flush();
vWriter.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment