En este video te enseño a crear un monitor de servicios de Windows con C# y Visual Studio
Código:
----------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace MonitorServicios
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void monitor()
{
//Creamos una instancia de ServiceController
try
{
ServiceController SQLServerServicio = new ServiceController("MSSQLSERVER");
//Obtenemos el estatus del servicio
if (SQLServerServicio.Status.ToString().Equals("Running"))
{
//cambiamos el color de fondo a verde
panel1.BackColor = Color.GreenYellow;
}
else
{
//Cambiamos el color de fondo a rojo
panel1.BackColor = Color.Red;
//Intentamos iniciar el servicio
try
{
SQLServerServicio.Start();
}
catch (Exception e)
{
//En caso de error mostramos un mensaje con el error
MessageBox.Show("Error:" + e);
}
}
}
catch (Exception e) {
//En caso de error mostramos un mensaje con el error
MessageBox.Show("Error:" + e);
//Cambiamos el color de fondo a rojo
panel1.BackColor = Color.Red;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
//Ejecutamos nuestro metodo
monitor();
//300000 = 5 minutos
//30000 = 30 segundos
}
private void btnSQLServer_Click(object sender, EventArgs e)
{
monitor();
}
}
}
-----------------------------------------------------------------------------------------------------------------------
Comentarios
Publicar un comentario