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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:20:21+00:00 2026-05-14T21:20:21+00:00

I’d like to start using the Task Parallel Library , as this is the

  • 0

I’d like to start using the Task Parallel Library, as this is the recommended framework going forward for performing asynchronous operations. One thing I haven’t been able to find is any means of forcible Abort, such as what Thread.Abort provides.

My particular concern is that I schedule tasks running code that I don’t wish to completely trust. In particular, I can’t be sure this untrusted code won’t deadlock and therefore I can’t be certain if a Task I schedule using this code will ever complete. I want to stay away from true AppDomain isolation (due to the overhead and complexity of marshaling), but I also don’t want to leave a Task thread hanging around, deadlocked. Is there a way to do this in TPL?

  • 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-14T21:20:22+00:00Added an answer on May 14, 2026 at 9:20 pm

    The way to do this is with a CancellationToken and the new cancellation model.
    The new cancellation model is integrated into the .NET Framework in several types. The most important ones are System.Threading.Tasks, System.Threading.Tasks.Task, System.Threading.Tasks.Task and System.Linq.ParallelEnumerable.

    Here’s an example of your problem. This code will always deadlock because the calling code takes a lock first and then the deadlocked task tries to aquire the same lock.

    public void Example()
    {
        object sync = new Object();
        lock (sync)
        {
            CancellationTokenSource canceller = new CancellationTokenSource();
        ManualResetEvent started = new ManualResetEvent(false);
            Task deadlocked = Task.Factory.StartNew(() => 
                { 
                started.Set();
                    // EVIL CODE: This will ALWAYS deadlock
                    lock(sync) { }; 
                }, 
                canceller.Token);
    
            // Make sure task has started.
        started.WaitOne(); 
    
            canceller.Cancel();
    
            try
            {
                // Wait for task to cancel.
                deadlocked.Wait();
            }
            catch (AggregateException ex) 
            {
                // Ignore canceled exception. SIMPLIFIED!
                if (!(ex.InnerException is TaskCanceledException))
                    throw;
            }
        }
    }
    

    Task cancellation in the TPL is cooperative. In other words this will always deadlock because nothing handles the cancellation token being set to cancelled because the task thread is locked.

    There is a way around this but it still relies on the authors of the untrusted code to do the right thing:

    public static void Example2()
    {
        Mutex sync = new Mutex(true);
    
        CancellationTokenSource canceller = new CancellationTokenSource();
        bool started = false;
    
        Task deadlocked = Task.Factory.StartNew(() =>
            {
                started = true;
                // EVIL CODE: This will ALWAYS deadlock 
                WaitHandle.WaitAny(new WaitHandle[] { canceller.Token.WaitHandle, sync });
            },
            canceller.Token);
    
        // Make sure task has started.
        while (!started) { }
    
        canceller.Cancel();
    
        try
        {
            // Wait for task to cancel. 
            deadlocked.Wait();
        }
        catch (AggregateException ex)
        {
            // Ignore canceled exception. SIMPLIFIED! 
            if (!(ex.InnerException is TaskCanceledException))
                throw;
        }
    } 
    

    Points to note; cancellation is cooperative. You can use Token.WaitHandle to get a handle and wait on it along with the handle(s) of other synchronization primitives. Mutex is much slower than Monitor (or lock).

    Really if you don’t trust the author of the code enough to have them implement cooperative cancellation then I’d question the sanity of having them run inside your AppDomain on the same thread.

    For further detail see:

    http://msdn.microsoft.com/en-us/library/dd997364.aspx

    http://msdn.microsoft.com/en-us/library/dd537607.aspx

    http://msdn.microsoft.com/en-us/library/ee191552.aspx

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

Sidebar

Ask A Question

Stats

  • Questions 447k
  • Answers 447k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Probably the best solution would be to rework the design… May 15, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer If you keep the post-receive hook, you could decide to… May 15, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer You should take a look at http://jqueryui.com/demos/tabs/, it should be… May 15, 2026 at 7:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.