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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:29:37+00:00 2026-05-29T23:29:37+00:00

I have a service running some different tasks in a loop until the service

  • 0

I have a service running some different tasks in a loop until the service is stopped.
However one of these tasks i calling a web service and this call can take several minutes to complete. I want to be able to stop the service instantly, ‘cancelling’ the web service call without calling Thread.Abort because that causes some strange behavior even if the only thing the thread is doing is calling this web service method.

How can i cancel or break from a synchronous method call (if it’s even possible)?
Or should I try a different approach?

I have tried to use the AutoResetEvent and then calling Thread.Abort which is working fine in the below code sample, but when implementing this solution in the actual service I get some unexpected behavior probably because of what’s going on in the external libraries I’m using.

AutoResetEvent and Thread.Abort:

class Program
{
    static void Main(string[] args)
    {
        MainProgram p = new MainProgram();
        p.Start();
        var key = Console.ReadKey();
        if (key.Key == ConsoleKey.Q)
            p.Stop();
    }
}

class MainProgram
{
    private Thread workerThread;
    private Thread webServiceCallerThread;
    private volatile bool doWork;

    public void Start()
    {
        workerThread = new Thread(() => DoWork());
        doWork = true;
        workerThread.Start();
    }

    public void Stop()
    {
        doWork = false;
        webServiceCallerThread.Abort();
    }

    private void DoWork()
    {
        try
        {
            while (doWork)
            {
                AutoResetEvent are = new AutoResetEvent(false);
                WebServiceCaller caller = new WebServiceCaller(are);
                webServiceCallerThread = new Thread(() => caller.TimeConsumingMethod());
                webServiceCallerThread.Start();

                // Wait for the WebServiceCaller.TimeConsumingMethod to finish
                WaitHandle.WaitAll(new[] { are });

                // If doWork has been signalled to stop
                if (!doWork)
                    break;

                // All good - continue
                Console.WriteLine(caller.Result);
            }
        }
        catch (Exception e)
        {
            Console.Write(e);
        }
    }
}

class WebServiceCaller
{
    private AutoResetEvent ev;
    private int result;

    public int Result
    {
        get { return result; }
    }

    public WebServiceCaller(AutoResetEvent ev)
    {
        this.ev = ev;
    }

    public void TimeConsumingMethod()
    {
        try
        {
            // Simulates a method running for 1 minute
            Thread.Sleep(60000);
            result = 1;
            ev.Set();
        }
        catch (ThreadAbortException e)
        {
            ev.Set();
            result = -1;
            Console.WriteLine(e);
        }
    }
}

Can someone suggest a solution to this issue?

  • 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-29T23:29:39+00:00Added an answer on May 29, 2026 at 11:29 pm

    The solution is really this simple: Don’t make calls that block for several minutes unless you want to block for several minutes. If there is no way to do a particular thing without blocking, potentially for several minutes, complain loudly to whoever wrote the code that imposes that painful requirement (or fix it yourself, if possible).

    Once you’ve made the call, it’s too late. You’re committed. If the function you are calling doesn’t provide a safe way to abort it, then there’s no safe way.

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

Sidebar

Related Questions

I have an echo web service running on lets say http://localhost:8080/axis2/services/Service1 . This service
I have some security related service running on my machine (start type = automatic)
I have a .NET Web Service running in VS2005 and a client that consumes
I have a .NET 3.5 based web service running at http://localhost/serivce.svc/ . Then I
I have an ASP.NET web service running that accepts both HTTP POST and SOAP
I have been trying to create a web service out some python scripts, and
need ask you about some help. I have web app running in Net 2.0.
I have a service that requires some input files from the client. This service
Ok we have a WCF service running on one machine on IIS6 + server2003
I have have running windows service that is using it's app.config for getting 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.