chinedu Nwankwo

chinedu Nwankwo

  • NA
  • 94
  • 29.5k

windows sevices not starting up with code

Jul 8 2014 2:54 AM
 
 
Please help i need urgent attention ! , i am working on a project to resart windows services with codes
 
i am selecting the service to be restarted from a dropdownlist box
 
each time i click on restart i get the error   : (particular service ) stopped , here is my code :
 
 
 
 
 

public partial class Homepage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillServiceName();
            }
        }

        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Label1.Text = DropDownList1.SelectedIndex.ToString();
            //Label1.Text = DropDownList1.SelectedIndex.ToString();
        }
        public void ServiceRestart(string nameofservice)
        {
            //Get the name of the service from the Web.config
            // string nameofservice = ConfigurationManager.AppSettings["service1"];
            ServiceController sv = new ServiceController(nameofservice);

            //Get service timeout from App.Config file
            int timeinmilliseconds = int.Parse(ConfigurationManager.AppSettings["timeinmilliseconds"].ToString());

            try
            {
                if (sv.Status.Equals(ServiceControllerStatus.Running) && sv.CanStop)
                {
                    int ms1 = Environment.TickCount;
                    int ms2;
                    TimeSpan timeout = TimeSpan.FromMilliseconds(timeinmilliseconds);
                    try
                    {
                        sv.Stop();
                        sv.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                        //Console.WriteLine("Service " + nameofservice + " stopped");
                        Label2.Text = "Services " + nameofservice + " stopped";
                        ms2 = Environment.TickCount;
                        timeout = TimeSpan.FromMilliseconds(timeinmilliseconds - (ms2 - ms1));
                    }
                    catch
                    {
                        Label2.Text = "Services " + nameofservice + "failed to stoped";
                    }

                    try
                    {
                        sv.Start();
                        Label2.Text = "Services" + nameofservice + "started";
                    }
                    catch
                    {
                        Label2.Text = "Services" + nameofservice + "failed to start";
                    }
                    sv.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    //Console.WriteLine("Service " + nameofservice + " started");
                    //Label2.Text="Services" + nameofservice + "started";
                }
                else
                {
                    sv.Start();
                    //Console.WriteLine("Service " + nameofservice + " started");
                    Label2.Text = "Services" + nameofservice + "started";
                }
            }

            catch (Exception e)
            {
                //Console.WriteLine("An error has occured: " + e.StackTrace);
                //Label2.Text = "Services" + nameofservice + "stopped";
                Label2.Text = "An error has occured: " + e.Message;
                //Re-start the service but with a longer timeout period
               //ServiceRestartAgain(nameofservice);

            }

        }


        //Another service
        public void ServiceRestartAgain(string nameofservice1)
        {
            //Get the name of the service from the Web.config
            //string nameofservice1 = ConfigurationManager.AppSettings["service1"];
            ServiceController sv1 = new ServiceController(nameofservice1);


            int timeinmilliseconds1 = int.Parse(ConfigurationManager.AppSettings["timeinmilliseconds2"].ToString());

            try
            {
                if (sv1.Status.Equals(ServiceControllerStatus.Running) && sv1.CanStop)
                {
                    int ms11 = Environment.TickCount;
                    TimeSpan timeout1 = TimeSpan.FromMilliseconds(timeinmilliseconds1);
                    sv1.Stop();
                    sv1.WaitForStatus(ServiceControllerStatus.Stopped, timeout1);
                    //Console.WriteLine("Service " + nameofservice1 + " stopped");
                   Label2.Text="Service " + nameofservice1 + "stopped";

                    int ms22 = Environment.TickCount;
                    timeout1 = TimeSpan.FromMilliseconds(timeinmilliseconds1 - (ms22 - ms11));
                    sv1.Start();
                    sv1.WaitForStatus(ServiceControllerStatus.Running, timeout1);
                    //Console.WriteLine("Service " + nameofservice1 + " started");
                    Label2.Text="Service " + nameofservice1 + "started";
                }

                else
                {
                    sv1.Start();
                    //Console.WriteLine("Service " + nameofservice1 + " started");
                    Label2.Text = "Service " + nameofservice1 + " started";
                }
            }

            catch (Exception e)
            {
                string stacktrace = e.StackTrace;
                //Console.WriteLine("An error has occured in restarting the service again: " + stacktrace);
                Label2.Text = e.Message;
                //Send mail to e-channels support(In the body of the mail put the stacktrace after adding the message. This is for tracking purposes.
               // MailEChannelsSupport(stacktrace);
            }

        }
// This loads up the list of services from the database 
 
 

        public void FillServiceName()
        {
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ATMrestartserviceConnectionString"].ConnectionString);

 

                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT * FROM Service", con);

                cmd.ExecuteNonQuery();
                SqlDataAdapter adap = new SqlDataAdapter(cmd);
                DataSet dats = new DataSet();
                adap.Fill(dats);
                DataTable dt = new DataTable();
                dt = dats.Tables[0];
                ListItem lstitem = new ListItem();
                lstitem = new ListItem();
                lstitem.Text = "Select Service";
                lstitem.Value = "s";
                drpservice.Items.Add(lstitem);
                foreach (DataRow datR in dt.Rows)
                {
                    lstitem = new ListItem();
                    lstitem.Text = datR["Service"].ToString();
                    lstitem.Value = datR["Service"].ToString();
                    drpservice.Items.Add(lstitem);

                }
                con.Close();
            }//end try
            catch (Exception ex)
            {

            }
        }

      
    }
}
 
Regards Chinedu Nwankwo

Answers (1)