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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:57:18+00:00 2026-06-04T02:57:18+00:00

All, I have a long running process that I run on a background thread

  • 0

All, I have a long running process that I run on a background thread (with cancellation support) using the Task Paralell Library (TPL). The code for this long running taks is contained within Class Validation, and when the method

public bool AsyncRunValidationProcess(TaskScheduler _uiScheduler, 
    CancellationToken _token, dynamic _dynamic = null)
{
    try
    {

        // Note: _uiScheduler is used to update the UI thread with progress infor etc.

        for (int i = 0; i < someLargeLoopNumber; i++)
        {
            // Cancellation requested from UI Thread.
            if (_token.IsCancellationRequested) 
                _token.ThrowIfCancellationRequested();
        }
        return true;
    }
    catch (Exception eX)
    {
        // Do stuff. Display `eX` if cancellation requested.
        return false;
    }
}

is run from Class Validation I can cancel the process fine. The cancellation request is handled by the appropriate delegate (shown below) and this works fine (I don’t belive this is the cause of my problem).

When I run this method from another class, Class Batch, I do this via a “controller” method

asyncTask = Task.Factory.StartNew<bool>(() => asyncControlMethod(), token);

which in turn invokes the method

valForm.AsyncRunValidationProcess(uiScheduler, token, 
    new List<string>() { strCurrentSiteRelPath }));

where valForm is my accessor to Class Validation, the method runs fine, but when I attempt a cancellation the delegate

cancelHandler = delegate 
{
    UtilsTPL.CancelRunningProcess(asyncTask, cancelSource);
};

where

public static void CancelRunningProcess(Task _task, 
    CancellationTokenSource _cancelSource)
{
    try
    {
        _cancelSource.Cancel();
        _task.Wait(); // On cross-class call it freezes here.
    }
    catch (AggregateException aggEx)
    {
        if (aggEx.InnerException is OperationCanceledException)
            Utils.InfoMsg("Operation cancelled at users request.");
        if (aggEx.InnerException is SqlException)
            Utils.ErrMsg(aggEx.Message);
    }
}

freezes/hangs (with no unhandled exception etc.) on _task.Wait(). This (I belive – through testing) is to do with the fact that I am cancelling asyncControlMethod() which has called valForm.AsyncRunValidationProcess(...), so it is cancelling asyncControlMethod() which is causing the current process to hang. The problem seems to be with passing the CancellationTokenSource etc. to the child method. The IsCancellationPending event fires and kills the controlling method, which causes the child method to hang.

Can anyone tell me what I am doing wrong or (more pertinently), what should I be doing to allow such a cancellation procedure?

Note: I have tried to spawn a child task to run valForm.AsyncRunValidationProcess(...), with its own CancellationToken but this has not worked.

Thanks for your time.

  • 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-04T02:57:20+00:00Added an answer on June 4, 2026 at 2:57 am

    The answer to this problem (helped massively by Jiaji Wu’s comment and link) was that you cannot declare the CancellationToken as a global variable that is passed to the cascading methods; that is, you cannot have

    public class MainClass
    {
        private CancellationTokenSource = source;
        private CancellationToken token;
    
        public MainClass()
        {
            source = new CancellationtokenSource();
            token = source.Token;
        }
    
        private void buttonProcessSel_Click(object sender, EventArgs e)
        {
            // Spin-off MyMethod on background thread.
            Task<bool> asyncControllerTask = null;
            TaskSpin(asyncControllerTask, cancelSource, token, MyMethod);
        }
    
        private void method()
        {
            // Use the global token DOES NOT work!
            if (token.IsCancellationRequested)      
                token.ThrowIfCancellationRequested();
        }
    
        private void TaskSpin(Task<bool> asyncTask, CancellationTokenSource cancelSource,
            CancellationToken token, Func<bool> asyncMethod)
        {
            try
            {
                token = cancelSource.Token;
                asyncTask = Task.Factory.StartNew<bool>(() => asyncMethod(token), token);
    
                // To facilitate multitasking the cancelTask ToolStripButton
                EventHandler cancelHandler = null;
                if (cancelSource != null)
                {
                    cancelHandler = delegate
                    {
                        UtilsTPL.CancelRunningProcess(mainForm, uiScheduler, asyncTask, cancelSource, true);
                    };
                }
    
            // Callback for finish/cancellation.
                asyncTask.ContinueWith(task =>
                {
                    // Handle cancellation etc.
                }
    
                // Other stuff...
            }
        }
    }
    

    Use of the global token in the maethod run on the background thread doen NOT work! The method must be explicitly passed the token for it to be able to register it. I am not sure of the exact reason why this is the case, but I will know in future, now you need to pass the token to MyMethod() like this

        private void method(CancellationToken token)
        {
            // Use the global token DOES NOT work!
            if (token.IsCancellationRequested)      
                token.ThrowIfCancellationRequested();
        }
    

    I hope this helps someone else.

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

Sidebar

Related Questions

I have a Perl script that executes a long running process and observes its
I have a long running process that integrates over a collection of incoming Entities
We have a long running data transfer process that is just an asp.net page
I have been using attachment_fu on a project for a long time and all
I have a $.getJSON call that's too long to post here. It all works
I have some long-running operations that number in the hundreds. At the moment they
I have a long running PHP script that has a memory leak which causes
I have a lisp program that needs to run for a long, long time.
I have a database-synchronisation task that takes some time to process, as there are
I have to get routing direction i.e. all the lat-long location between two location

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.