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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:45:40+00:00 2026-05-16T17:45:40+00:00

What would be the best approach for implementing Tasks with a KEY that operate

  • 0

What would be the best approach for implementing Tasks with a KEY that operate as follows:-

Option 1) Only one of this key is ever pending. Can be used, for example, from ASP.NET MVC to queue up a single render for a thumbnail image no matter how many times the image Url is hit. Only one runs, all other requests wait for that one to complete.

Option 2) All items with the same key must execute sequentially. Can be used for example to ensure that operations that fetch a file from a backing store to a local cache don’t all try to get the file to the cache at the same time. Option 1 is a special case of this where subsequent actions with the same key are simply discarded (typically saves just a file-exists-check).

I have an existing WorkQueue that handles both of these cases (as well as Apartment state, ThreadPriority settings and maximum degrees of parallelism). TPL appears to be the best solution for replacing this and will bring improved cancellation options.

Nested Tasks with continuations look hopeful but maintaining a dictionary of currently queue’d tasks soon gets messy between the TaskFactory and the TaskScheduler classes. Inheriting from Task is problematic too since neither TaskFactory nor TaskScheduler are generic on Task.

Most Task Parallel examples assume that the set of tasks is known ahead of time. In this case new tasks are added all the time and need to be either discarded or chained onto existing tasks depending on the operation requested and the key passed in.

Has anyone implemented anything similar to this using TPL and if so, what approach did you take in your Task, TaskScheduler and TaskFactory classes?

  • 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-16T17:45:40+00:00Added an answer on May 16, 2026 at 5:45 pm

    Perhaps, one way that I can think of is

    1. Create a wrapper class – say KeyProcessor to queue up items for a key.
    2. KeyProcessor.Run() method will be capable of whatever queuing semantics that you need for. Essentially it would look for internal queue for any pending work and then keep doing it sequentially.
    3. Maintain the dictionary of KeyProcessor objects.
    4. For any new task, check in the dictionary for the same key. If does not exist then add it. Queue the task on it. If its not running then schedule it with TPL using Run method as action.
    5. Use ContinueWith to schedule maintainer task – for example, whenever Task executing KeyProcessor.Run is completed, continuation tasks can check if there are any more tasks been scheduled for the same key (since it has completed) and start it again OR remove from dictionary.

    All above would have been tricky from thread sync point not for few interesting collections present in System.Collections.Concurrent namespace. This would make the above logic much more simpler. For example, ConcurrentDictionary.GetOrAdd will allow to lookup and/or add the KeyProcessor object in thread-safe way.

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

Sidebar

Related Questions

What would be the best algorithm for finding a number that occurs only once
What would be the best approach to install a keyboard hook on Linux (X-windows)
Would you recommend me the best approach to edit SQL query with Visual Studio
When requesting hardware for a WebLogic server, what hardware would best improve its performance?
I have the following situation I think would be best to show in sample
In Java, what would the best way be to have a constantly listening port
I was looking at WF and was wondering if it would be best to
What would be the best way to have a list of items with a
What would be the best way to fill a C# struct from a byte[]
What would be the best version control system to learn as a beginner to

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.