Crear e instalar un servicio de Windows con C# y Visual Studio
En este video te muestro como crear e instalar un servicio de Windows con C# y Visual Studio para que puedas ejecutar tareas en un servicio que se ejecute en segundo plano
Comando para instalar servicio
1. cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
Código
--------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ServicioOne
{
[RunInstaller(true)]
public partial class Service1 : ServiceBase
{
int tiempo = Settings1.Default.Tiempo;
public Thread worker = null;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
ThreadStart start = new ThreadStart(Working);
worker = new Thread(start);
worker.Start();
}
catch (Exception)
{
throw;
}
}
public void Working()
{
int i = 0;
while (true)
{
string path = @"C:\\ejemplo.txt";
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine("Linea " + i);
}
i++;
Thread.Sleep(tiempo * 60 * 1000);
}
}
protected override void OnStop()
{
try
{
if (worker != null && worker.IsAlive)
{
worker.Abort();
}
}
catch (Exception)
{
throw;
}
}
}
}
--------------------------------------------------------------------------------------------------------------------------------
Comentarios
Publicar un comentario