Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7873419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:30:32+00:00 2026-06-03T02:30:32+00:00

i have a code to restart a service in an event which does other

  • 0

i have a code to restart a service in an event which does other functions too.

I have a try catch in the event for everything within the event like this:

private void btnApply_Click(object sender, EventArgs e)
    {
        try
        {               
            applyChangesAndCheckRestartService();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error loading page.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

private void applyChangesAndCheckRestartService()
    {
        string svrPortNo = CommonCodeClass.getTagValue(CommonCodeClass.xml_SvrPortNoTag, CommonCodeClass.configLocation + CommonCodeClass.configXML);               

        if (!ApplyChangesForSettings())
        {                
            return;
        }

        if (svrPortNo != tbSvrPortNo.Text)
        {               
            CommonCodeClass.CheckToRestartService();
        }
    }

Now if there is an error during ApplyChangesForSettings() i will get an error popup “Error loading page”.

If there is an error in CheckToRestartService() i will get the same error because of the try catch.

Is there a better way to handle this.

Like i dont mind the error loading page for ApplyChangesForSettings() but for CheckToRestartService() i would like to see an error like “unable to restart service”.

Any suggestions are appreciated.
Thanks

internal static void CheckToRestartService()
    {
        DialogResult result = MessageBox.Show(CommonCodeClass.resartServiceMessage, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (result == DialogResult.Yes)
        {
            CommonCodeClass.RestartService(CommonCodeClass.serviceName, 60000);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:30:33+00:00Added an answer on June 3, 2026 at 2:30 am

    Do they throw different exceptions? If they do you could use exception filtering:

    private void btnApply_Click(object sender, EventArgs e)
    {
        try
        {               
            applyChangesAndCheckRestartService();
        }
    
        // catch service start exceptions
        catch (InvalidOperationException ioex)
        {
            // display message that couldn't start service
        }
    
        // catch rest
        catch (Exception ex)
        {
            MessageBox.Show("Error loading page.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    

    UPDATE this is assuming you’re calling something like ServiceController.Start() which throws InvalidOperationException on failure, you could easily throw this yourself on your own error condition or create your own custom exception.

    if (/* service didn't start */)
    {
        throw new InvalidOperationException("Could not start service.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that references a web service, and I'd like the address of
I have this code to restart a service, but this is not working. I
I have the following lines of code in my .js file $(triggers.restart).live('click', function (e)
I have code which is updating a model's property then calling save! . A
I have code like <?php header('Content-Type: text/plain; charset=UTF-8'); echo 'Hello Loréane !'; the e
I have code that basically does the following to read in properties that are
I have code which imports an xml feed in opencart. I can clear the
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T
I have a program written in VB.NET which stops a service that uses file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.