OneCompiler

Windows Application - Vijay

153

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.Windows.Forms;

namespace WinFormsApp1
{
public partial class Form1 : Form
{
ProcessLog processLog = null;
ErrorLog errorLog = null;
public Form1()
{
InitializeComponent();
}

	private void button1_Click(object sender, EventArgs e)
	{
		processLog = new ProcessLog();

		try
		{
			ServiceController serviceController = new ServiceController("MyWindowsService");
			var Status = serviceController.Status.ToString();
			processLog.ProcessLogFile("Window App Run : Status :- " + Status);
			if (serviceController.Status != ServiceControllerStatus.Running && serviceController.Status != ServiceControllerStatus.StartPending)
			{
				TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
				serviceController.Start();
				serviceController.WaitForStatus(ServiceControllerStatus.Running, timeout);
			}
		}
		catch (Exception ex)
		{
			errorLog = new ErrorLog();
			errorLog.ErrorlogFile(ex.Message);
		}
	}

	private void button2_Click(object sender, EventArgs e)
	{
		processLog = new ProcessLog();

		try
		{
			ServiceController serviceController = new ServiceController("MyWindowsService");
			var Status = serviceController.Status.ToString();
			processLog.ProcessLogFile("Window App Run : Status :- " + Status);
			if (serviceController.Status == ServiceControllerStatus.Running)
			{
				TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
				serviceController.Stop();
				serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

			}
		}
		catch (Exception ex)
		{
			errorLog = new ErrorLog();
			errorLog.ErrorlogFile(ex.Message);
		}
	}
}

}