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

  • Home
  • SEARCH
  • 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 8457975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:57:11+00:00 2026-06-10T12:57:11+00:00

I have this code running a powershell script if my service is starting or

  • 0

I have this code running a powershell script if my service is starting or stopped.

Timer timer1 = new Timer();

ServiceController sc = new ServiceController("MyService");

protected override void OnStart(string[] args)
    {
        timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        timer1.Interval = 10000;
        timer1.Enabled = true;
    }

    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        if ((sc.Status == ServiceControllerStatus.StartPending) || (sc.Status ==  ServiceControllerStatus.Stopped))
        {
            StartPs();
        }
    }

    private void StartPs()
    {
        PSCommand cmd = new PSCommand();
        cmd.AddScript(@"C:\windows\security\dard\StSvc.ps1");
        PowerShell posh = PowerShell.Create();
        posh.Commands = cmd;
        posh.Invoke();
    }

It’s working fine when I kill my service from cmd prompt
But even if my service is up and running, the powershell script continues to execute itself (it appends a file on the computer)
Any idea why ?

  • 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-10T12:57:13+00:00Added an answer on June 10, 2026 at 12:57 pm

    The ServiceController.Status property is not always live; it is lazily evaluated the first time it is requested, but (unless requested) only that time; subsequent queries to Status will not normally check the actual service. To force this, add:

    sc.Refresh();
    

    before your .Status check:

    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        sc.Refresh();
        if (sc.Status == ServiceControllerStatus.StartPending ||
            sc.Status == ServiceControllerStatus.Stopped)
        {
            StartPs();
        }
    }
    

    Without that sc.Refresh(), if it was Stopped (for example) initially, it will always say Stopped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code running in my onDestroy function: @Override protected void onDestroy() {
I have this code running great in ff, opera and chrome: <script type=text/javascript> $(document).ready(function(){
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
On server side I have this code which running in new thread static void
I have this code running from an external script for an image slider for
Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
I have been using this code, but have been running into some memory issues:
I'm a beginner with jdbc ... I have a problem running this code :
i have this code running to load my images all asynch and show a
I have this code: a = xyz g = abcd & a After running

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.