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

  • Home
  • SEARCH
  • 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 9008365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:55:15+00:00 2026-06-16T01:55:15+00:00

I want to implement a prioritised ActionBlock<T> . So that i can Conditionally give

  • 0

I want to implement a prioritised ActionBlock<T>. So that i can Conditionally give priority to some TInput items by using a Predicate<T>.
I read Parallel Extensions Extras Samples and Guide to Implementing Custom TPL Dataflow Blocks.
But Still don`t figure out how can i implement this scenario.
—————————- EDIT —————————
There are some tasks, which 5 of them can be run simultaneously. When user push the button, some (depends on predicate function) tasks should run with the most priority.
In fact i write this code

TaskScheduler taskSchedulerHighPriority;
ActionBlock<CustomObject> actionBlockLow;
ActionBlock<CustomObject> actionBlockHigh;
...
queuedTaskScheduler = new QueuedTaskScheduler(TaskScheduler.Default, 5);
taskSchedulerHigh = queuedTaskScheduler.ActivateNewQueue(0);
taskSchedulerLow = queuedTaskScheduler.ActivateNewQueue(1);
...
actionBlockHigh = new ActionBlock<CustomObject>(new Action<CustomObject>(method), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 5, SingleProducerConstrained = false, TaskScheduler = taskSchedulerHigh });
actionBlockLow = new ActionBlock<CustomObject>(new Action<CustomObject>(method), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 5, MaxMessagesPerTask = 1, TaskScheduler = taskSchedulerLow });
...     
if (predicate(customObject))
    actionBlockHigh.Post(customObject);
else
    actionBlockLow.Post(customObject);

But it seems priority does not take effected at all.
—————————- EDIT ——————
I find the fact that when i use this line of code:

actionBlockHigh = new ActionBlock<AvlHistory>(new Action<AvlHistory>(SemaphoreAction), new ExecutionDataflowBlockOptions { TaskScheduler = taskSchedulerHigh });
actionBlockLow = new ActionBlock<AvlHistory>(new Action<AvlHistory>(SemaphoreAction), new ExecutionDataflowBlockOptions { TaskScheduler = taskSchedulerLow });

Cause application observe priorities of the Tasks correctly but only one task can be execute at a time, meanwhile using the first code block that is shown in flowing, cause application run 5 tasks simultaneously but in inappropriate priority order.

actionBlockHigh = new ActionBlock<AvlHistory>(new Action<AvlHistory>(SemaphoreAction), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 5, TaskScheduler = taskSchedulerHigh });
actionBlockLow = new ActionBlock<AvlHistory>(new Action<AvlHistory>(SemaphoreAction), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 5, TaskScheduler = taskSchedulerLow });

Update:
Tanks to svick, i should specify MaxMessagesPerTask for taskSchedulerLow.

  • 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-06-16T01:55:17+00:00Added an answer on June 16, 2026 at 1:55 am

    Your question doesn’t include many details, so the following is just a guess of what you might need.

    I think the simplest way to do this is to have two ActionBlocks, running on different priorities on QueuedTaskScheduler from ParallelExtensionsExtras. You would link to the high-priority one using a predicate and then to the low-priority one. Also, to make sure high-priority Tasks aren’t waiting, set MaxMessagesPerTask of the low-priority block.

    In code, it would look something like:

    static ITargetBlock<T> CreatePrioritizedActionBlock<T>(
        Action<T> action, Predicate<T> isPrioritizedPredicate)
    {
        var buffer = new BufferBlock<T>();
    
        var scheduler = new QueuedTaskScheduler(1);
    
        var highPriorityScheduler = scheduler.ActivateNewQueue(0);
        var lowPriorityScheduler = scheduler.ActivateNewQueue(1);
    
        var highPriorityBlock = new ActionBlock<T>(
            action, new ExecutionDataflowBlockOptions
            {
                TaskScheduler = highPriorityScheduler
            });
        var lowPriorityBlock = new ActionBlock<T>(
            action, new ExecutionDataflowBlockOptions
            {
                TaskScheduler = lowPriorityScheduler,
                MaxMessagesPerTask = 1
            });
    
        buffer.LinkTo(highPriorityBlock, isPrioritizedPredicate);
        buffer.LinkTo(lowPriorityBlock);
    
        return buffer;
    }
    

    This is just a sketch of what you could do, for example, Completion of the returned block doesn’t behave correctly.

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

Sidebar

Related Questions

I want to implement some action when CTRL-A is pressed . How can I
I want to implement SQLite database for iPhone using PhoneGap. I know some basics
I want implement a program that can do a a Vision-based Page Segmentation. I
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement a simple script to do some boring housekeeping on my
I want to implement login using facebook in my windows phone 7.1 application When
I want to implement the shopping cart by using multidimensional session array but don't
I wrote small service class and I want implement something like that : service
I want to implement a userland command that will take one of its arguments
everyone! My english is poor and sorry fot that. I want implement a function

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.