Bueno una función que nos puede ayudar en determinado momento es listar los servicios activos de Windows un ejemplo sencillo de como hacerlo es hacer usando System.Serviceprocess (NO olvides agregarlo como referencia al proyecto):
Imports System
Imports System.ServiceProcess
Class Program
Private Shared Sub Main()
Dim services As ServiceController() = ServiceController.GetServices()
Console.WriteLine("Listado de los servicios corriendo : ")
For Each service As ServiceController In services
If service.Status = ServiceControllerStatus.Running Then 'cambiar por ServiceControllerStatus.Stopped para ver inactivos
Console.WriteLine()
Console.WriteLine(" Nombre de servicio: {0}", service.ServiceName)
Console.WriteLine(" Nombre mostrado: {0}", service.DisplayName)
End If
Next
End Sub
End Class
y en c# sería así.
using System;
using System.ServiceProcess;
class Program
{
static void Main()
{
ServiceController[] services = ServiceController.GetServices();
Console.WriteLine("Listado de los servicios corriendo : ");
foreach (ServiceController service in services)
{
if (service.Status == ServiceControllerStatus.Running) //cambiar por ServiceControllerStatus.Stopped para ver inactivos
{
Console.WriteLine();
Console.WriteLine(" Nombre de servicio: {0}",
service.ServiceName);
Console.WriteLine(" Nombre mostrado: {0}",
service.DisplayName);
}
}
}
}
para saber los procesos que están inactivos usamos la siguiente propiedad.
ServiceControllerStatus.Stopped.
A continuación, este código te ayudará para evitar que un formularion se cierre sin previa validación, para esto usamos la evento FormClosing de los formulacion de Windows Forms.
En Visual Basic .Net es de esta manera
Private Sub forma_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
Dim valor1 As [Decimal]
Dim valor2 As [Decimal]
valor1 = Convert.ToDecimal(txtval1.Text)
valor2 = Convert.ToDecimal(txtval2.Text)
If valor1 <> valor2 Then
MessageBox.Show(Me, "Los valore deben coincidir", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
e.Cancel = True 'evita que el form se cierre
End If
End Sub
private void forma_FormClosing(object sender, FormClosingEventArgs e)
{
Decimal valor1;
Decimal valor2;
valor1 = Convert.ToDecimal(valor1.Text);
valor2 = Convert.ToDecimal(valor2.Text);
if (valor1 != valor2)
{
MessageBox.Show(this, "Los valores deben coincidir", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
e.Cancel = true; //evita que el form se cierre
}
}
Un código bastante útil en el tema de validaciones y prohibiciones para que esas ocaciones que se necesita aplicar seguridad en los formularios.
Bueno estas funciones ayudan a validar si el texto ingresado en un textbox es un valor numérico ó es un valor decimal, estas son de las validaciones más usadas para desarrollar así que acá se los dejo.
Public Shared Function EsInteger(ByVal theValue As String) As Boolean
'funcion para enteros
Try
Convert.ToInt32(theValue)
Return True
Catch
Return False
End Try
End Function
Public Shared Function EsDecimal(ByVal theValue As String) As Boolean
'funcion para enteros
Try
Convert.ToDecimal(theValue)
Return True
Catch
Return False
End Try
End Function
public static bool EsInteger(string theValue)//funcion para enteros
{
try
{
Convert.ToInt32(theValue);
return true;
}
catch
{
return false;
}
}
public static bool EsDecimal(string theValue)//funcion para enteros
{
try
{
Convert.ToDecimal(theValue);
return true;
}
catch
{
return false;
}
}
otra manera de validar de parte de @cmsalvado gracias por el dato:
Public Shared Function EsInteger(ByVal theValue As String) As Boolean
Dim value As Integer
Return Integer.TryParse(theValue, value)
End Function
public static bool EsInteger(string theValue)
{
int value;
return int.TryParse(theValue, out value);
}