Exemple #1
0
        public static AsyncTask <R> BeginTask <R>(AsyncTask <R> function)
        {
            R    retv      = default(R);
            bool completed = false;

            object sync = new object();

            IAsyncResult asyncResult = function.BeginInvoke(
                iAsyncResult =>
            {
                lock (sync)
                {
                    completed = true;
                    retv      = function.EndInvoke(iAsyncResult);
                    Monitor.Pulse(sync);
                }
            }, null);

            return(delegate
            {
                lock (sync)
                {
                    if (!completed)
                    {
                        Monitor.Wait(sync);
                    }
                    return retv;
                }
            });
        }
Exemple #2
0
        public override void Run()
        {
            int                   threadId;
            AsyncTask             d = Test;
            IncompatibleAsyncTask e = Test;

            Console.WriteLine($"Option 1");
            Task <string> task = Task <string> .Factory.FromAsync(
                d.BeginInvoke("AsyncTaskThread", Callback,
                              "delegate async call"), d.EndInvoke);

            task.ContinueWith(t =>
            {
                Console.WriteLine($"Callback finish result : {t.Result}");
            });

            while (!task.IsCompleted)
            {
                Console.WriteLine(task.Status);
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
            }

            Console.WriteLine(task.Status);
            Thread.Sleep(TimeSpan.FromSeconds(1));

            Console.WriteLine("----------------------------------");
            Console.WriteLine();
            Console.WriteLine("Option 2");

            task = Task <string> .Factory.FromAsync(
                d.BeginInvoke, d.EndInvoke, "AsyncTaskThread", "delegate async call"
                );

            task.ContinueWith(t =>
                              Console.WriteLine($"Task comp Result : {t.Result}"));

            while (!task.IsCompleted)
            {
                Console.WriteLine(task.Status);
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
            }
            Console.WriteLine(task.Status);
            Thread.Sleep(TimeSpan.FromSeconds(1));

            Console.WriteLine("--------------------------");
            Console.WriteLine();
            Console.WriteLine($"Option 3");

            IAsyncResult ar = e.BeginInvoke(out threadId, Callback, $"delegate async call");

            task = Task <string> .Factory.FromAsync(ar, _ => e.EndInvoke(out threadId, ar));

            task.ContinueWith(t =>
                              Console.WriteLine($"Task comp Result : {t.Result}, Thread id : {threadId}"));

            while (!task.IsCompleted)
            {
                Console.WriteLine(task.Status);
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
            }
            Console.WriteLine(task.Status);
            Thread.Sleep(TimeSpan.FromSeconds(1));
        }