Friday, 26 January 2018

Simple Multi Tasking program in C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; 

namespace multitasking
{
    class Program
    {
        static void Main(string[] args)
        {
            execute();
            Console.WriteLine("end of the program");
            Console.Read();
        } 

        static async Task execute()
        {
            Program p1 = Activator.CreateInstance(typeof(multitasking.Program)) as Program;
            Task t = p1.run1();
            Task t1 = p1.run2();
            await Task.WhenAll(t, t1);
            Console.WriteLine("end of the program class");
        }

        async Task run1()
        {
            await Task.Run(() =>
             {
                 for (int i = 0; i < 100; i++)
                 {
                     Console.WriteLine("Run 1:" + i);
                     Task.Delay(5000);
                 }
             });
        }

        async Task run2()
        {
            await Task.Run(() =>
             {
                 for (int i = 0; i < 100; i++)
                 {
                     Console.WriteLine("Run 2:" + i);
                     Task.Delay(1000);
                 }
             });
        }
    }
}

 

No comments:

Post a Comment