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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:11:09+00:00 2026-05-31T14:11:09+00:00

I have the following task cancelSource = new CancellationTokenSource(); token = cancelSource.Token; string strDbA

  • 0

I have the following task

cancelSource = new CancellationTokenSource();
token = cancelSource.Token;

string strDbA = textBox1.Text;
string strDbB = textBox2.Text;

// Start duplication on seperate thread. 
asyncDupSqlProcs =
    new Task<bool>(state =>
        UtilsDB.DuplicateSqlProcsFrom(token, mainForm.mainConnection, strDbA, strDbB), "Duplicating SQL Proceedures");
asyncDupSqlProcs.Start();
asyncDupSqlProcs.ContinueWith(task =>
{
    switch (task.Status)
    {
        // Handle any exceptions to prevent UnobservedTaskException.             
        case TaskStatus.Faulted:
            // Error-handling logic...
            break;
        case TaskStatus.RanToCompletion:
            if (asyncDupSqlProcs.Result)
                Utils.InfoMsg(String.Format(
        "SQL stored procedures and functions successfully copied from '{0}' " + 
                    "to '{1}'", strDbA, strDbB));
            break;
        case TaskStatus.Canceled:
            Utils.InfoMsg("Copy cancelled at users request.");
            break;
    }
}, TaskScheduler.FromCurrentSynchronizationContext());

In the method DuplicateSqlProcsFrom(token, mainForm.mainConnection, strDbA, strDbB) I have the standard cancellation detection:

if (_token.IsCancellationRequested)
    _token.ThrowIfCancellationRequested();

The cancellation event is a button click on the main form, inside the click event I have:

try
{
    cancelSource.Cancel();
    asyncDupSqlProcs.Wait();
}
catch (AggregateException aggEx)
{
    if (aggEx.InnerException is OperationCanceledException)
        Utils.InfoMsg("Copy cancelled at users request.");
}

but I can seem to catch the AggregateException, what am I doing wrong here?

Edit: Inside the method DuplicateSqlProcsFrom(token, mainForm.mainConnection, strDbA, strDbB) I can catch the OperationCancelledException but I am confused about how to handle it. All the example I have seen handle the printing of “Operation Cancelled…” etc. on the UI thread within the event that caused the cancel. What is the best way to capture the cancel and pass it back to the UI/calling thread?

  • 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-31T14:11:11+00:00Added an answer on May 31, 2026 at 2:11 pm

    To get an OperationCancelledException, it needs to be thrown with the same token as the one passed to the Task’s constructor:

    new Task<bool>(state =>
        UtilsDB.DuplicateSqlProcsFrom(token, mainForm.mainConnection, strDbA, strDbB),
            "Duplicating SQL Proceedures", token);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: CancellationTokenSource cancelSource = new CancellationTokenSource(); _cancelTokenList.Add(cancelSource); CancellationToken token =
I have the following task in my MSBuild script: <Target Name=ZipStates> <Message Text=CREATING ZIP
Guys I'm new to xml in Java. I have the following task. I need
I have following string task BLABLA@{taskId} @{BLABLA.title} and want to extract all placeholders from
I have the following ant task: <target name=test> <replace file=test.txt token=smth value=anything/> </target> test.txt
I have the following code: var task = Task.Factory.StartNew(CheckFiles, cancelCheckFile.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); private void
I have the following Data Flow task in my SSIS package: alt text http://img228.imageshack.us/img228/358/ssis1.png
I have the following task, which because of the combination of DestinationFiles and DestionationFolder
I have the following beans Task, ServerDetails and ApplicationDetails. I wish to retrieve all
I have the following situation: var Task = Backbone.Model.extend({ initialize: function() { }, save:

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.