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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:05:48+00:00 2026-05-25T14:05:48+00:00

I am developing a WPF app in C# where I have an Uri that

  • 0

I am developing a WPF app in C# where I have an Uri that I want to download Json data. The code will deserialize the downloaded Json data to object, thereafter, the object has a list of Uris that would require to request more Json data which i would like to request in parallel. And the downloaded Json data might have more Uris to request. The code should be able to do WebClient.CancelAsync on the parent and its children’s WebClient when desired. I am looking at the Task Parallel Library and finding it difficult to grasp and implement it. I’m unsure if I should use the TPL’s Cancellation token to invoke the CancelAsync or the CancelAsync to cancel the TPL’s Cancellation token. And I not sure if i should use nested Tasks for the children WebClient….?

If anyone has a similar scenario and implemented it using the new TPL.. would you mind sharing a snippet of the code…

Thanks.

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

    If I can suggest using Reactive Extensions (Rx) on top of the TPL then this can be done quite easily without the need for cancellation tasks etc.

    If I can assume you have the following:

    // The initial Uri
    Uri initialUri = ...;
    
    // A function to return the JSON string from a given Uri
    Func<Uri, string> getJason = ...; 
    
    // Turn the JSON into the next set of Uris to fetch
    Func<string, IEnumerable<Uri>> getUris = ...; 
    

    Then, using Rx, you turn these functions into functions that return observables using the Task Pool, like so:

    Func<Uri, IObservable<string>> getJasonObsFunc = u =>
        Observable
            .FromAsyncPattern<Uri, string>(
                getJason.BeginInvoke,
                getJason.EndInvoke)
            .Invoke(u)
            .ObserveOn(Scheduler.TaskPool);
    
    Func<string, IObservable<Uri>> getUrisObsFunc = j =>
        Observable
            .FromAsyncPattern<string, IEnumerable<Uri>>(
                getUris.BeginInvoke,
                getUris.EndInvoke)
            .Invoke(j)
            .ObserveOn(Scheduler.TaskPool)
            .SelectMany(xs => xs.ToObservable());
    

    You’ll need a callback to get the Uri/JSON pairs out. Something like this:

    Action<Uri, string> callback = (u, j) =>
        Console.WriteLine(String.Format("{0} => {1}", u, j));
    

    Here’s the recursive LINQ query that will recursively fetch each JSON string:

    Func<Uri, IObservable<Uri>> getAllUris = null;
    getAllUris = u =>
        Observable
            .Return<Uri>(u)
            .Merge(
                from j in getJasonObsFunc(u).Do(k => callback(u, k))
                from u1 in getUrisObsFunc(j)
                from u2 in getAllUris(u1)
                select u2);
    

    Then you invoke all of this goodness using the following line:

    var subscription = getAllUris(initialUri).Subscribe();
    

    Now, if you want to cancel the query execution just call this:

    subscription.Dispose();
    

    Rx handles all of the tasks and cancels them all for you.

    I hope this helps.

    Here are the links for Rx:

    • Reactive Extensions Forum
    • Reactive Extensions (Rx) v1.0.10621
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a WPF desktop app for a small business. It will have
I'm developing a WPF app using MVVM. Most of my views have only xaml
i'm developing an application (wpf) that have 3 windows. in the main window user
I'm currently developing a WPF application in C# and I want to have a
I have a WPF TaskBar like application that i am developing for fun and
I'm developing WPF applications using MVVM pattern. I have ViewModel with code like this:
I am developing a WPF kiosk like client that will be deployed on an
I am developing a wpf app. I have a database like this. I have
I am developing a WPF app that contains a webbrowser control that loads a
I am developing a WPF desktop app that uses Entity Framework 4 and SQL

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.