.NET 4.0 Threading.Tasks

I recently started work on a new application that will use the parallelism task. I was just starting to write a task structure, but recently I saw a series of posts about SO regarding a new System.Threading.Tasks namespace, which may be useful to me (and I would prefer to use the existing structure, except for the movie).

However, while looking at MSDN, I did not see how / if I can implement the functionality I'm looking for:

  • Dependence on other tasks.
  • You can wait for an unknown number of tasks that precede the same action (possibly wrapped in the same object task, which is called several times)
  • Set maximum matching task instances, since they use a shared resource, there is no point running more than once
  • Priority hint or scheduler sets tasks with lower maximum parallel instances with higher priority (to maximize the use of the specified resource)
  • Change the ability to change the priority of tasks that perform the same action (a pretty bad example, but PredictWeather (Tommorrow) will have a higher priority than PredictWeather (NextWeek))

Can someone point me to an example / tell me how I can achieve this? Greetings.

C # Use Case: (printed in SO, so please for syntax errors / typos)

** note Do()/ DoAfter()should not block the calling thread *

class Application ()
{

    Task LeafTask = new Task (LeafWork) {PriorityHint = High, MaxConcurrent = 1};
    var Tree = new TaskTree (LeafTask); 

    Task TraverseTask = new Task (Tree.Traverse); 
    Task WorkTask = new Task (MoreWork); 
    Task RunTask = new Task (Run);

    Object SharedLeafWorkObject = new Object ();

    void Entry ()
    {
        RunTask.Do ();
        RunTask.Join (); // Use this thread for task processing until all invocations of RunTask are complete
    }

    void Run(){                           
        TraverseTask.Do ();

        // Wait for TraverseTask to make sure all leaf tasks are invoked before waiting on them 
        WorkTask.DoAfter (new [] {TraverseTask, LeafTask});

        if (running){
            RunTask.DoAfter (WorkTask);   // Keep at least one RunTask alive to prevent Join from 'unblocking'
        }
        else    
        {
            TraverseTask.Join();
            WorkTask.Join ();
        }
    } 

    void LeafWork (Object leaf){
        lock (SharedLeafWorkObject)  // Fake a shared resource
        {
            Thread.Sleep (200); // 'work'
        } 
    }

    void MoreWork ()
    {
        Thread.Sleep (2000); // this one takes a while
    }
}


class TaskTreeNode<TItem>
{
    Task LeafTask; // = Application::LeafTask
    TItem Item;

    void Traverse ()
    {
        if (isLeaf)
        { 
            // LeafTask set in C-Tor or elsewhere
            LeafTask.Do(this.Item);

            //Edit
            //LeafTask.Do(this.Item, this.Depth); // Deeper items get higher priority
            return;
        }
        foreach (var child in this.children)
        {
            child.Traverse (); 
        }
    }
}
+3
source share

Source: https://habr.com/ru/post/1727205/


All Articles