Friday, February 27, 2015
Nested And Child Task
using System.Windows.Forms;
namespace NestedAndChildTask
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStartNestedTask_Click(object sender, EventArgs e)
{
//create an outer task
//add to it a couple to nested tasks
//In Lambda expression (() => { }); represent Action Delegate
Task ParentTask = Task.Factory.StartNew(() =>
{
SetText("Parent task started execution", richTextBox1);
//Start a task within the parentTask. Such Task is therefore a nested task
Task innerTask1 = Task.Factory.StartNew(() =>
{
SetText("Child task1 started execution", richTextBox1);
//make this child tasks do something, like spinning
for (int i = 1; i <= 10; i++)
{
System.Threading.Thread.SpinWait(150000000);
}
SetText("Child task1 Comleting.", richTextBox1);
});
//Another child task 2================
Task innerTask2 = Task.Factory.StartNew(() =>
{
SetText("Child task2 started execution", richTextBox1);
//make this child tasks do something, like spinning
for (int i = 1; i <= 10; i++)
{
System.Threading.Thread.SpinWait(150000000);
}
SetText("Child task2 Comleting.", richTextBox1);
});
});
ParentTask.ContinueWith(task =>
{
//At this point in time the parenttask has completed its work
SetText("Parent task completing.", richTextBox1);
});
}
//=====================Method to set Text=======================
//1. define a delegate with same signature as the crosss-threading handling
// method (the SetText method)
private delegate void RichTextBoxHandler(string s, RichTextBox rtb);
private void SetText(string s, RichTextBox rtb)
{
if (rtb.InvokeRequired)
{
//create a delegate object
RichTextBoxHandler handler = SetText;
Invoke(handler, s, rtb);
}
else
{
rtb.AppendText(s + "\n");
//scroll down
rtb.ScrollToCaret();
}
}
private void btnStartChildTask_Click(object sender, EventArgs e)
{
Task parentTask = Task.Factory.StartNew(() =>
{
SetText("Parent task started execution", richTextBox1);
//Start a task within the parentTask. Such Task is therefore a nested task
Task childTask1 = Task.Factory.StartNew(() =>
{
SetText("Child task1 started execution", richTextBox1);
//make this child tasks do something, like spinning
for (int i = 1; i <= 10; i++)
{
System.Threading.Thread.SpinWait(150000000);
}
SetText("Child task1 Comleting.", richTextBox1);
}, TaskCreationOptions.AttachedToParent);
//Another child task 2================
Task childTask2 = Task.Factory.StartNew(() =>
{
SetText("Child task2 started execution", richTextBox1);
//make this child tasks do something, like spinning
for (int i = 1; i <= 10; i++)
{
System.Threading.Thread.SpinWait(150000000);
}
SetText("Child task2 Comleting.", richTextBox1);
}, TaskCreationOptions.AttachedToParent);
});//This is added for child task.
parentTask.ContinueWith(task =>
{
//At this point in time the parenttask has completed its work
SetText("Parent task completing.", richTextBox1);
});
//Because both inner tasks are attached to parent
//both tasks are considered child tasks, which implies that the parent
//WILL NOT terminate until all of its child tasks completes
}
}
}
////msdn.microsoft.com/en-us/library/vstudio/dd997417(v=vs.100).aspx
//Lab Assignment
//Define 2 methods similar to the ones we did before
//GetTotalofOddRandomValues and GetTotalOfEvenRandomValues (may add them as lambda expression)
//Run each method in a separate tasks (task1 and task2)
//Both tasks should run inside a parent task
//The parent task is to get the return values of both task1 and task2, add them and
//display the result
//
//P.S: the methods should not display anyting. you may add a longer loop to makes each
//method run for few seconds
Labels:
C#Advance
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment