using System; using System.Collections.Generic; using System.ComponentModel;
using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Threading; using System.Threading.Tasks; using
System.Windows.Forms; namespace winformLearning { public partial class Task study :
Form { public Task study () { InitializeComponent(); } private void
button1_Click(object sender, EventArgs e) { // Create a task Task<int> task = new
Task<int>(() => { int sum = 0; Console.WriteLine(" use Task Performing asynchronous operations .");//------2 for
(int i = 0; i < 100; i++) { Thread.Sleep(100); sum += i; } return sum; });
// Start task , And schedule to the current task queue thread to execute tasks (System.Threading.Tasks.TaskScheduler) task.Start();
Console.WriteLine(" The main thread performs other processing "); //----1 // Execute processing on task completion . Task cwt =
task.ContinueWith(t => { //Thread.Sleep(5000);
Console.WriteLine(" Execution results after task completion :{0}", t.Result.ToString());//------4 });
task.Wait(); Console.WriteLine("task end of execution ");//-----3 cwt.Wait();
Console.WriteLine("task End of callback execution ");//-------5 } public void TastMethod1(string
name) { Thread.Sleep(6000); Console.WriteLine("name:"+name); } public void
TastMethod2(string name) { Thread.Sleep(3000); Console.WriteLine("name:" +
name); } } }
 

 

Technology