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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:45:49+00:00 2026-05-31T07:45:49+00:00

I have a windows service developed in C#. On it’s Start method I have

  • 0

I have a windows service developed in C#. On it’s Start method I have a initialization such as:

Task _backgroundTask = null;

CancellationTokenSource _backgroundCancellationSource = null;

protected override void OnStart(string[] args)
{

......

_backgroundCancellationSource = new CancellationTokenSource();

CancellationToken token = backgroundCancellationSource.Token;

_backgroundTask = new Task(() => BackgroundFoldersProcessing(token), token, TaskCreationOptions.LongRunning);

.......
}

Now the method BackgroundFoldersProcessing looks like this:

void BackgroundFoldersProcessing(CancellationToken token)
{

            while (true)
            {
                try
                {
                    if (token.IsCancellationRequested)
                    {                        
                        return;
                    }

                    DoSomeWork()
                }
                catch (Exception ex)
                {
                    .........
                }
            }
}

Now, the Stop method is as follows:

protected override void OnStop()
{
.................

_backgroundCancellationSource.Cancel();
_backgroundTask.Wait();

_backgroundCancellationSource.Dispose();
_backgroundTask.Dispose();

_backgroundTask = null;
_backgroundCancellationSource = null;

.................
}

Now the problem is when I try to stop the service in a middle of processing, the Wait method of _backgroundTask would not stop the service until and unless the DoSomeWork() method inside the BackgroundFoldersProcessing gets completed, the Windows Service would not stop.

Is there any way, though which I can stop the service and the execution of _backgroundTask would be terminated, even though the DoSomeWork() method gets completed/executed or not? I have also tried token.ThrowIfCancellationRequested() in BackgroundFoldersProcessing method, but that also did not worked. I want that whenever I try to Stop the service from Service Control Manager (SCM), the service should be stopped immediately and the __backgroundTask should stop executing the BackgroundFoldersProcessing method and be terminated as well. How can I achieve this?

  • 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-31T07:45:50+00:00Added an answer on May 31, 2026 at 7:45 am

    You can try use ThreadAbortException:

    defining the thread:

    ThreadStart threadDelegate = new ThreadStart(BackgroundFoldersProcessing);
    Thread thread_ = new Thread(threadDelegate);
    thread_.Start();
    

    Add catch to BackgroundFoldersProcessing

    catch (ThreadAbortException e)
    {
         return;
    }
    

    and when you want to shut it down use:

    thread_.Abort();
    thread_.Join();
    

    Then when Abort() will be called ThreadAbortException will be thrown.

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

Sidebar

Related Questions

I have a simple windows Service developed in VS.net 2008 and VB.net. When I
I have a .net Windows Service developed in VB.net. I have a settings file
I have developed a Windows service using Visual Studio 2008. I want to install
I have a very simple windows Service that is developed in vb.net 2008. When
I have developed a Windows service and this is service is running on my
I have developed a .NET Windows Service (in VS2010) that needs to: Access shared
I have a windows service application developed in C#. The same service needs to
I have developed a Windows service along with a setup project using Visual Studio
I have developed a Windows service in C#. I have created a installer with
I have developed an app that runs as a windows service and regularly does

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.