C# Multi Threading Session 3 (Return values from thread)

preview_player
Показать описание
Return values from thread
Рекомендации по теме
Комментарии
Автор

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CreateThread
{
    class Program
    {
        static void Main(string[] args)
        {
            Task<string> t=new Task<string>(PerformWork);
            t.Start();
            Console.WriteLine(t.Result);
            Thread.Sleep(1000);
           
            Console.WriteLine(t.Result);
            Thread.Sleep(1000);
            Func<string> func = PerformWork;
            IAsyncResult res = func.BeginInvoke(null, null);
            received asynchronously");
            Thread.Sleep(1000);
            func.BeginInvoke(Done, func);
            Thread.Sleep(1000);
            MyThread thread = new MyThread();
            thread.Start();
           
            Console.ReadLine();
        }
        static string PerformWork()
        {
            return "Success result from static method on thread";
        }
        static void Done(IAsyncResult res)
        {
            Func<string> func = (Func<string>)res.AsyncState;
            " printed by cllback");
        }
        class MyThread
        {
            string result;
            Thread thread;
            public string Result
            {
                get
                {
                    thread.Join();
                    return result;
                }
            }
            public void Start()
            {
                thread = new Thread(this.PerformWork);
                thread.Start();
            }
            public void PerformWork()
            {
                result = "Success result from instance method on thread";
            }
        }
    }
}

santoshKoolkarni