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 1088477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:04:31+00:00 2026-05-16T23:04:31+00:00

I am a complete beginner to working with windows services. I have a basic

  • 0

I am a complete beginner to working with windows services. I have a basic skeleton worked out for the service and I am currently doing this:

protected override void OnStart(string[] args)
    {
        base.OnStart(args);
        Process.Start(@"someProcess.exe");
    }

just to fire-off the exe at the start of the program.

However, I’d like to have the service stop itself when the process started from the exe quits.
I’m pretty sure I need to do some sort of threading (something I am also a beginner with), but I’m not sure of the overall outline of how this works, nor the specific way to stop a process from within itself.
Could you help me with the general process for this (i.e. start a thread from OnStart, then what…?)? Thanks.

  • 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-05-16T23:04:32+00:00Added an answer on May 16, 2026 at 11:04 pm

    You can use a BackgroundWorker for the threading, use Process.WaitForExit() to wait for the process to terminate until you stop your service.

    You’re right that you should do some threading, doing lots of work in the OnStart may render errors about not starting correctly from Windows when starting the service.

    protected override void OnStart(string[] args)
    {
    
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerAsync();
    }
    
    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo("file.exe");
        p.Start();
        p.WaitForExit();
        base.Stop();
    }
    

    Edit
    You may also want to move the Process p to a class member and stop the process in the OnStop to make sure that you can stop the service again if the exe goes haywire.

    protected override void OnStop()
    {
        p.Kill();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I am a beginner in Android development; I have been doing a
Ok so I am a complete beginner and have managed to put together a
I am a complete beginner trying to develop for FCKeditor so please bear with
As a complete beginner with no programming experience, I am trying to find beautiful
I'm a complete beginner when it comes to programming. I'm taking a stab at
First, let me say that I'm a complete beginner at Python. I've never learned
I am a complete JSP beginner. I am trying to use a java.util.List in
The complete warning is Validation (): Element 'html' occurs too few times This is
I have a complete XML document in a string and would like a Document
I'll start by saying I am very much a beginner programmer, this is essentially

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.