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 ();
}
void Run(){
TraverseTask.Do ();
WorkTask.DoAfter (new [] {TraverseTask, LeafTask});
if (running){
RunTask.DoAfter (WorkTask);
}
else
{
TraverseTask.Join();
WorkTask.Join ();
}
}
void LeafWork (Object leaf){
lock (SharedLeafWorkObject)
{
Thread.Sleep (200);
}
}
void MoreWork ()
{
Thread.Sleep (2000);
}
}
class TaskTreeNode<TItem>
{
Task LeafTask;
TItem Item;
void Traverse ()
{
if (isLeaf)
{
LeafTask.Do(this.Item);
return;
}
foreach (var child in this.children)
{
child.Traverse ();
}
}
}
source
share