C#.Task.SimpleExamples

Tasks executed in thread pool asyncroniously.

ex. 1 Returning no result

        static void Main(string[] args)
        {
            Task t = new Task(() => Console.WriteLine("this is task"));
            t.Start();
            t.Wait();
        }

ex. 2 Returning result


        static void Main(string[] args)
        {
            Task<int> t = new Task<int>((obj) => (int) obj, 123);
            t.Start();
            t.Wait();
            Console.WriteLine(t.Result);
        }

ex.3 Returning Result and using function

        static void Main(string[] args)
        {
            Task<int> t = new Task<int>(() => Sum(1, 2));
            t.Start();
            t.Wait();
            Console.WriteLine(t.Result);
        }

        static int Sum(int a, int b)
        {
            return a + b;
        }

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.