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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:52:08+00:00 2026-05-30T03:52:08+00:00

Background In my utilities library (Shd.dll) I have a class called AsyncOperation. To put

  • 0

Background

In my utilities library (Shd.dll) I have a class called AsyncOperation. To put it simply, it’s a base class for types that encapsulate a potentially long running operation, executes it on a background thread, and it supports pause/resume, cancellation and progress reporting. (It’s like a BackgroundWorker, just knows more things.)

In the user code you can use it like this:

class MyOperation : AsyncOperation
{
    public MyOperation() : base(null, AsyncOperationOptions.Cancelable | AsyncOperationOptions.Pausable) {}

    protected override void RunOperation(AsyncOperationState operationState, object userState)
    {
         ...
         operationState.ThrowIfCancelled();
    }
}

 var op = new MyOperation();
 op.Start();
 ...
 op.Cancel();

operationState.ThrowIfCancelled() does exactly what its name suggests: if Cancel() was called earlier by another thread, it throws an internal exception (AsyncOperationCancelException), which is then handled by the AsyncOperation type, like this:

private void _DoExecute(object state)
{
    // note that this method is already executed on the background thread
    ...
    try
    {
        operationDelegate.DynamicInvoke(args); // this is where RunOperation() is called
    }
    catch(System.Reflection.TargetInvocationException tiex)
    {
        Exception inner = tiex.InnerException;
        var cancelException = inner as AsyncOperationCancelException;
        if(cancelException != null)
        {
             // the operation was cancelled
             ...
        }
        else
        {
            // the operation faulted
            ...
        }
        ...
    }
    ...
}

This works perfectly. Or so I thought for the past year, while I was using this in numerous scenarios.

The actual problem

I’m building a class that uses System.Net.WebClient to upload potentially large number of files via FTP. This class is built using the AsyncOperation base class as described above.

For accurate progress reports, I use WebClient.UploadFileAsync(), which complicates the code, but the relevant parts look like this:

private ManualResetEventSlim completedEvent = new ManualResetEventSlim(false);

private void WebClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
    ...
    if (OperationState.IsCancellationRequested)
    {
        _GetCurrentWebClient().CancelAsync();
    }
}

private void WebClient_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
    ...
    _UploadNextFile();
}

private void _UploadNextFile()
{
    if (OperationState.IsCancellationRequested || ...)
    {
        this.completedEvent.Set();
        return;
    }
    ...
}

protected override void RunOperation(AsyncOperationState operationState, object userState)
{
    ...
    _UploadNextFile();
    this.completedEvent.Wait();

    operationState.ThrowIfCancelled(); // crash
    ...
}

As you can see, I marked the line where the crash occurs. What exactly happens is that when execution hits that line (I put a break point right over it, so I know this is the exact line), Visual Studio 2010 freezes for about 15 seconds, and then the next thing I see is the source code of AsyncOperationState.ThrowIfCancelled():

public void ThrowIfCancelled()
{
    if(IsCancellationRequested)
    {
        throw new AsyncOperationCancelException();
    }
} // this is the line the debugger highlights: "An exception of type AsyncOperationCancelException' occured in Shd.dll but was unhandled by user code."

I tried putting breakpoints to where the exception should have been caught, but the execution never reaches that catch {} block.

The other weird this is that at the end it also writes the following: “Function evaluation disabled because a previous function evaluation timed out.” I Googled this problem, and tried everything that was suggested (disabled implicit property evaluation, removed all breakpoints), but nothing helped so far.

Here are two screenshots that illustrate the problem:
http://dl.dropbox.com/u/17147594/vsd1.png
http://dl.dropbox.com/u/17147594/vsd2.png

I’m using .NET 4.0. Any help would be very much appreciated.

  • 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-30T03:52:10+00:00Added an answer on May 30, 2026 at 3:52 am

    When the Visual Studio debugger is attached to an application, it gets notified whenever an exception is thrown, before the running code gets the chance to handle it. This is called a first-chance exception, and VS can be configured to break execution when a certain exception type is thrown.

    You can specify debugger behavior for each exception type separately using the Exceptions window (Debug menu). By default, all exceptions have the “User-unhandled” checkbox checked, meaning that only unhandled exceptions will break execution. Setting the “Thrown” checkbox for a certain exception type forces VS to break execution even if the exception will be handled, but only for that exception type (not for derived types). If a handler exists, once you resume execution (by pressing F5), the exception will be caught normally.

    I would guess that your custom exception was added to the list of exceptions in the Exceptions window (which you can check by using the Find button inside the window).

    [Edit]

    According to my tests, it also happens when DynamicInvoke is used in .NET 4, regardless of the Exceptions window setting. Yesterday I was using VS2008 and I couldn’t reproduce it, but it does seem like odd behavior now.

    This is the test I tried (sorry for the brief formatting, but it’s fairly simple):

    Action<int> a = i => { throw new ArgumentException(); };
    
    // When the following code is executed, VS2010 debugger
    // will break on the `ArgumentException` above 
    // but ONLY if the target is .NET 4 (3.5 and lower don't break)
    try { a.DynamicInvoke(5); }
    catch (Exception ex)
    { }
    
    // this doesn't break
    try { a.Invoke(5); }
    catch (Exception ex)
    { }
    
    // neither does this
    try { a(5); }
    catch (Exception ex)
    { }
    

    My only guess is that exception handling done inside InvokeMethodFast (which is an InternalCall method) has somehow changed. DynamicInvoke code has changed between versions 4 and prior, but there is nothing which would indicate why VS2010 debugger is unable to see that there is an exception handler inside that method call.

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

Sidebar

Related Questions

Real quick background : We have a PDFMaker (HTMLDoc) that converts html into a
Background I have an oracle database table with a lot of columns that I'm
Background We are developing some in-house utilities using ASP.NET 2.0. One of which is
Background: I have a little video playing app with a UI inspired by the
Background: At my company we are developing a bunch applications that are using the
Background I have a massive db for a SharePoint site collection. It is 130GB
Background I am trying to create a copy of a business object I have
Background: I couldn't find any decent free HTML to PDF conversion utilities in C#.
I have an app that starts playing sounds and begins/resumes gameplay in the onResume()
I have an MVC 2 application that utilises forms. The required fields within the

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.