2

I am firing off a Task of on a background thread pool thread via the following method

private async Task LoadCoreMatchDataAsync()
{
    string errorMessage = String.Empty;
    ...
    try
    {
        if (!HasConnection)
            return;

        IProgress<string> progressIndicator = new Progress<string>(LoadProgress);
        EventsCollection = new BindableCollection<Taurus.MatchDetails>(
            await MatchDataService.GetCollectionAsync(
                this.client, progressIndicator, this.token));
        ...
    }
    catch (TimeoutException toe)
    {
        errorMessage = String.Format(
            "Retrieval of the MatchDetails using connection " +
            "\"{0}\" failed with the following TimeoutException: \"{1}\".",
            this.ConnectionString,
            toe.Message);
    }
    catch (OperationCanceledException)
    {
        // Do nothing, cancel silently.
    }

    // Display any errors.
    if (!String.IsNullOrEmpty(errorMessage))
    {
        await dialogManager.ShowDialog<MessageDialogResult>(
            new MessageBoxViewModel("Connection Timeout", errorMessage, mbs));
        HasConnection = false;
    }
}

where the GetCollectionAsync(...) method is

public async Task<BindableCollection<Taurus.MatchDetails>> GetCollectionAsync(
    MongoClient client, IProgress<string> progressIndicator, CancellationToken token)
{
    return await Task.Factory.StartNew<BindableCollection<Taurus.MatchDetails>>(() =>
        {
            ... // Somewhere in here we get a TimeoutException thrown. 
        }, token);
}

the problem is that when in my call to await MatchDataService.GetCollectionAsync(...) I get an expected TimeoutException, VS2012 throws "a TimeoutException was unhandled by user code" message, when clearly I am handling the exception in the "continuation" in the correct way. If I continue rather than break, the exception is indeed caught and I get my expected error message. I am just not sure why VS2012 is telling me that the exception is unahandled?

I am essentially doing what is described clearly in on of Jon Skeets answers here https://stackoverflow.com/a/19865613/626442.

Thanks for your time.

Community
  • 1
  • 1
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • 2
    Does the behavoiur change if you used `Task.Run` instead of `Task.Factory.StartNew`? The former is usually more suitable. – NeddySpaghetti Feb 18 '15 at 10:51
  • 1
    I will eventually be using a custom scheduler, so this is the reason I have not used `Task.Run` but you are of course right. However, switching it will make no difference. – MoonKnight Feb 18 '15 at 10:57
  • Can you test the same with a try-catch inside of your `GetCollectionAsync` method? And throw the exception from the catch block. – Piyush Feb 18 '15 at 13:35

1 Answers1

1

You have "Just My Code" turned on (which I mentioned in my answer in the other question you reference). The debugger is indicating that "User code" (Your code) did not handle the exception--which is true. The exception is caught by the framework code and placed into a Task.

Turn off the "Just My Code" debugger setting (in my opinion, it is a feature that only causes confusion and has very limited usefulness).

Matt Smith
  • 17,026
  • 7
  • 53
  • 103
  • 1
    You are welcome. It causes other annoying behavior too: in the call stack rather than seeing the true call stack you'll see portions of it replaced with [External Code]. – Matt Smith Feb 18 '15 at 15:42