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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:05:08+00:00 2026-06-02T06:05:08+00:00

I have a C# Windows service. When I try to stop it goes in

  • 0

I have a C# Windows service. When I try to stop it goes in stopping state and when I placed the break point in onStop method then it is not getting hit. Not able to debug what is problem. I have added exception handling all over the service and also unhandled exception handling but I don’t get any error. Every thing is fine with the servie it has canStop set to true and also uses RequestAdditionalTime().

I have also set legacyUnhandledExceptionPolicy enabled=”true” in app.config.
For me it looks like some threading/resource issue that prevent service control manager to call the OnStop method. Or the service may be in already corrupt state and service control manager can not stop it. It just change the state from Stop to Stopping.

When I try debugging by attaching code to service exe nothing happens when try to stop the service. The onstop method is not getting called.

In Onstart method starts a thread that keep doing a kind of polling on every 30 second. Suppose onStop method gets called then it will abort this thread. This is how this service is developed.

Is there some free tools available to debug if there is some threading issue or my thread in OnStart method is chocking the resource available to service control manager?

Code in OnStop method:

try
{
    RequestAdditionalTime(10*60*1000);
    IntPtr handle = this.ServiceHandle;
    KServiceStatus.currentState = (int) State.SERVICE_START_PENDING;
    SetServiceStatus(handle, ref KServiceStatus);

    RequestAdditionalTime(30000);

    if ((onStartThread == null) ||
         ((onStartThread.ThreadState &
          (ThreadState.Unstarted | ThreadState.Stopped)) != 0))
    {
        onStartThread = new Thread(new ThreadStart(KWindowsServiceHandler.OnStart));
        onStartThread.Start();
    }

    KServiceStatus.currentState = (int) State.SERVICE_RUNNING;
    SetServiceStatus(handle, ref KServiceStatus);
 }              

OnStartThread goes on running until service is running. It required to do a polling.
If onStop method is call (which is not being called) will abort this OnStartThread.

  • 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-02T06:05:12+00:00Added an answer on June 2, 2026 at 6:05 am

    Main problem was to find what the problem is. So went on commenting and uncommenting different part of code and found the real issue.OnStartthread launches a process ( which starts one exe) So i kept just this code in the my windows service and removed everything and i was able to reproduce the issue.so what I did is let the OnStartthread sleep for 1s after starting the process. ( the more appropriate would be to start the process and catch the different events to know the status of the process and when it is ready just move) it worked like magic.
    This is somehow fixes the issue that i was facing.

    When this sleep was not there then process would start but I was not able to hit the break point on OnStop method because the process (exe) got started but it needed some more resource to start fully and be consistent and putting the thread.sleep give the required resource to exe to start fully.

        //the code is in OnStartThread 
    

    someProcess.start();
    Thread.Sleep(1000);

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

Sidebar

Related Questions

I have a Windows Service running on a .Net 2.0 environment (NOT UPGRADABLE TO
I have a windows service, in which I want a top level try-catch that
I have a windows service developed in C#. On it's Start method I have
I have created a C# web service using visual studio to stop the windows
I have a Windows Service that exposes a WCF service and so I'm not
I have a Windows service (C#) where I create multiple threads which will try
I have a Windows Service which performs a certain function, and then needs to
I try to do the following: I have a Windows service which is running
I have a windows service that has a timer that fires a method every
I have a windows service which will start and stop the execution of some

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.