コード例 #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Main invoked on thread: {0}", Thread.CurrentThread.ManagedThreadId);

            BinaryOp myAddOp = new BinaryOp(Add);

            // compute result on secondary thread
            IAsyncResult ar = myAddOp.BeginInvoke(10, 5, null, null);

            // do work until the secondary thread is completed
            while (!ar.IsCompleted)
            {
                Console.WriteLine("Doing more work in Main()");
                Thread.Sleep(1000);
            }

            // obtain the result
            int result = myAddOp.EndInvoke(ar);

            Console.WriteLine("Here's the result: {0}", result);

            // using WaitOne()
            // WaitOne() blocks the current thread for set amount of time
            // if the async call completes within wait time true is returned else false
            ar = myAddOp.BeginInvoke(20, 40, null, null);

            while (!ar.AsyncWaitHandle.WaitOne(1000, true))
            {
                Console.WriteLine("Doing more work in Main()");
            }
            result = myAddOp.EndInvoke(ar);
            Console.WriteLine("Here's the result: {0}", result);

            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Async Delegate Invocation *****");

            //  输出正在执行中的线程的ID
            Console.WriteLine("Main() invoked on thread {0}. ",
                              Thread.CurrentThread.ManagedThreadId);

            //  在次线程中调用Add()
            BinaryOp     b     = new BinaryOp(Add);
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

            //  在Add()方法完成之前会一直显示消息
            //while (!iftAR.IsCompleted)
            //{
            //    Console.WriteLine("Doing more work in Main()!");
            //    Thread.Sleep(1000);
            //}
            while (!iftAR.AsyncWaitHandle.WaitOne(1000, true))
            {
                Console.WriteLine("Doing more work in Main()!");
            }

            //  当执行完后获取Add()方法的结果
            int answer = b.EndInvoke(iftAR);

            Console.WriteLine("10 + 10 is {0}.", answer);

            Console.ReadLine();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: usedflax/flaxbox
        static void Main(string[] args)
        {
            Console.WriteLine("***** Async Delegate Invocation *****");

            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.",
              Thread.CurrentThread.ManagedThreadId);

            BinaryOp b = new BinaryOp(Add);
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

            // This message will keep printing until
            // the Add() method is finished.
            while (!iftAR.IsCompleted)
            {
                Console.WriteLine("Doing more work in Main()!");
                Thread.Sleep(1000);
            }

            // Now we know the Add() method is complete.
            int answer = b.EndInvoke(iftAR);

            Console.WriteLine("10 + 10 is {0}.", answer);
            Console.ReadLine();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Async Delegate Invokation *****\n");

            //  print out the ID of the executing thread
            Console.WriteLine($"Main() invoked on thread { Thread.CurrentThread.ManagedThreadId }.");

            //  invoke Add() on a secondary thread
            BinaryOp     b  = new BinaryOp(Add);
            IAsyncResult ar = b.BeginInvoke(10, 20, null, null);

            //  once these statements are processed the calling thread is blocked until BeginInvoke() completes

            //  do other work on primary thread
            //  check status of Add() method and return when ar.IsCommpleted
            //while (!ar.IsCompleted)
            while (!ar.AsyncWaitHandle.WaitOne(1000, true))
            {
                Console.WriteLine($"Doing more work in Main()!");
                //Thread.Sleep(1000);
            }

            //  obtain the result of the Add() method when ready
            int answer = b.EndInvoke(ar);

            Console.WriteLine($"10 + 10 = { answer }.");


            Console.Write($"\n\nPress <Enter> to Exit. . .");
            Console.ReadLine();
        }
コード例 #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Async Delegate Invocation *****");

            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.",
                              Thread.CurrentThread.ManagedThreadId);

            BinaryOp     b  = new BinaryOp(Add);
            IAsyncResult ar = b.BeginInvoke(10, 10, null, null);

            // This message will keep printing until
            // the Add() method is finished.
            //while (!ar.IsCompleted)
            //{
            //    Console.WriteLine("Doing more work in Main()!");
            //    Thread.Sleep(1000);
            //}
            while (!ar.AsyncWaitHandle.WaitOne(1000, true))
            {
                Console.WriteLine("Waiting for the async call to complete...");
            }

            // Now we know the Add() method is complete.
            Console.WriteLine(ar.IsCompleted);
            int answer = b.EndInvoke(ar);

            Console.WriteLine("10 + 10 is {0}.", answer);
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Synch Delegate Review *****");
            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId);

            BinaryOp binaryOp = new BinaryOp(Add);
            // Once the next statement is processed,
            // the calling thread is now blocked until
            // BeginInvoke() completes.
            IAsyncResult iftAr = binaryOp.BeginInvoke(10, 20, null, null);

            // Do other work on primary thread... This call takes far less than five seconds!
            //Console.WriteLine("Doing more work in Main()!");

            // This message will keep printing until
            // the Add() method is finished.
            while (!iftAr.IsCompleted)
            {
                Console.WriteLine("Doing more work in Main()!");
                Thread.Sleep(1000);
            }

            //while (!iftAr.AsyncWaitHandle.WaitOne(1000, true)) //maximum wait time
            //{
            //    Console.WriteLine("Doing more work in Main()!");
            //}

            // Now we are waiting again for other thread to complete!
            int answer = binaryOp.EndInvoke(iftAr);

            Console.WriteLine(answer);
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: maziesmith/.NetCSharp
        static void Main(string[] args)
        {
            Console.WriteLine("***** Async Delegate Invocation *****");

            //print out ID of executing thread
            Console.WriteLine($"Main() invoked on thread {Thread.CurrentThread.ManagedThreadId}");

            //Invoke the Add() method on secondary thread
            BinaryOp     b     = new BinaryOp(Add);
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

            while (!iftAR.IsCompleted)
            {
                //Do other work on primary thread...
                Console.WriteLine("Doing more work in Main()");
                Thread.Sleep(1000);
            }

            //obtain the result of Add()
            //Method when ready
            int answer = b.EndInvoke(iftAR);

            Console.WriteLine($"10 + 10 is {answer}");
            Console.Read();
        }
コード例 #8
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Async Delegate Invocation *****");

            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.",
                              Thread.CurrentThread.ManagedThreadId);

            // Invoke Add() on a secondary thread.
            BinaryOp     b     = new BinaryOp(Add);
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);


            // This message will keep printing until
            // the Add() method is finished.
            while (!iftAR.IsCompleted)
            {
                Console.WriteLine("Doing more work in Main()!");
                Thread.Sleep(1000);
            }
            // Now we know the Add() method is complete.
            int answer = b.EndInvoke(iftAR);


            Console.WriteLine("10 + 10 is {0}.", answer);
            Console.ReadLine();
        }
コード例 #9
0
        // Asyc nature of delegates
        private static void AsyncNatureOfDelegates()
        {
            Console.WriteLine("***** Async Delegate Invocation *****");

            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId);

            // Invoke Add() on a secondary thread.
            BinaryOp     b     = new BinaryOp(Add);
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

            //// Do other work on primary thread...
            //Console.WriteLine("Before Add : Doing more work in Main()!");

            // This message will keep printing until
            // the Add() method is finished.
            while (!iftAR.IsCompleted)
            {
                Console.WriteLine("Doing more work in Main()! while Add() is getting executed.");
                Thread.Sleep(1000);
            }

            // Obtain the result of the Add()
            // method when ready.
            int answer = b.EndInvoke(iftAR);

            Console.WriteLine("10 + 10 is {0}.", answer);

            // Do other work on primary thread...
            Console.WriteLine("After Add : Doing more work in Main()!");
        }
コード例 #10
0
        static void Example2()
        {
            Console.WriteLine("****** Async Delegate Invocation ******");

            // Print out the ID of the executing thread.

            Console.WriteLine("Main()  invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId);

            // Invoke Add() on secondary thread.

            BinaryOp b = new BinaryOp(Add);

            IAsyncResult ar = b.BeginInvoke(10, 10, null, null);

            //Do other work on primary thread ....
            //this message will keep printing until
            //the Add() method is finished.

            while (!ar.IsCompleted)
            {
                Console.WriteLine("Doing more work in Main()!");
                Thread.Sleep(1000);
            }

            //while (!ar.AsyncWaitHandle.WaitOne(1000, true))
            //{
            //    Console.WriteLine("Doing more work in Main()!");
            //}

            // Now we know the Add() method is completed.
            int answer = b.EndInvoke(ar);

            Console.WriteLine("10 +10 is {0}.", answer);
            Console.ReadLine();
        }
コード例 #11
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Async Delegate Invocation *****");
            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId);
            // Invoke Add() on a secondary thread.
            BinaryOp     b  = new BinaryOp(Add);
            IAsyncResult ar = b.BeginInvoke(10, 10, null, null);

            // Do other work on primary thread...
            Console.WriteLine("\nDoing more work in Main()...\n");
            //while (!ar.IsCompleted)
            while (!ar.AsyncWaitHandle.WaitOne(500, true))
            {
                Console.WriteLine("Doing more work in Main()...");
                //Thread.Sleep(500);
            }
            Console.WriteLine("\n Work in Background Thread Completed \n");
            // Obtain the result of the Add() method when ready.
            int answer = b.EndInvoke(ar);

            Console.WriteLine("10 + 10 is {0}.", answer);

            Console.ReadLine();
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: sergeo74/LearningCSharp
        static void Main(string[] args)
        {
            Console.WriteLine("***** Async Delegate Invocation *****");

            // Вывести идентификатор выполняющегося потока.
            Console.WriteLine("Main() invoked on thread {0}.",
                              Thread.CurrentThread.ManagedThreadId);

            // Вызвать Add() во вторичном потоке.
            BinaryOp     b           = new BinaryOp(Add);
            IAsyncResult asyncResult = b.BeginInvoke(10, 10, null, null);

            // Это сообщение продолжит выводиться до тех пор,
            // пока не будет завершен метод Add() .
            while (!asyncResult.AsyncWaitHandle.WaitOne(1000, true))
            {
                Console.WriteLine("Doing more work in Main()!");
            }

            // По готовности получить результат выполнения метода Add().
            int answ = b.EndInvoke(asyncResult);

            Console.WriteLine("10 + 10 is {0}", answ);
            Console.ReadLine();
        }
コード例 #13
0
ファイル: Program.cs プロジェクト: wordtinker/c-sharp
        static void WaitCall()
        {
            Console.WriteLine("***** Async Delegate Review *****");
            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.",
                              Thread.CurrentThread.ManagedThreadId);
            // Invoke Add() in a asynchronous manner.
            BinaryOp b = new BinaryOp(Add);

            IAsyncResult res = b.BeginInvoke(10, 10, null, null);

            //while (!res.IsCompleted)
            //{
            //    Console.WriteLine("Doing more work in Main()!");
            //    Thread.Sleep(1000);
            //}
            while (!res.AsyncWaitHandle.WaitOne(1000, true))
            {
                Console.WriteLine("Doing more work in Main()!");
            }

            //Obtain results from Add
            int answer = b.EndInvoke(res);

            Console.WriteLine("10 + 10 is {0}.", answer);
        }
コード例 #14
0
        static void Main(string[] args)
        {
            Console.WriteLine("*****  Async Delegate Invocation *****");

            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.",
                              Thread.CurrentThread.GetHashCode());

            BinaryOp     b     = new BinaryOp(Add);
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

//            while(!iftAR.IsCompleted)
//            {
//                Console.WriteLine("Doing more work in Main()!");
//            }

            while (!iftAR.AsyncWaitHandle.WaitOne(2000, true))
            {
                Console.WriteLine("Doing more work in Main()!");
            }

            // Now we know the Add() method is complete.
            int answer = b.EndInvoke(iftAR);

            Console.WriteLine("10 + 10 is {0}.", answer);
            Console.ReadLine();
        }
コード例 #15
0
        static void Main(string[] args)
        {
            Console.WriteLine("***Synch Delegate Review");

            Console.WriteLine("main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);

            BinaryOp     b     = new BinaryOp(Add);
            IAsyncResult iftAr = b.BeginInvoke(10, 10, null, null);

            int counter = 0;

            //while (!iftAr.IsCompleted) //finchè sarà falso, l'altro thread non sarà finito
            //{
            //    Console.WriteLine("Doing more work in Main(), counter = {0}", counter);
            //    Thread.Sleep(1000);
            //    counter++;
            //}

            while (!iftAr.AsyncWaitHandle.WaitOne(1000, true)) //più raffinato
            {
                Console.WriteLine("Doing more work in Main(), counter = {0}", counter);
                counter++;
            }

            int answer = b.EndInvoke(iftAr);

            Console.WriteLine("10 + 10 is {0}", answer);
            Console.ReadLine();
        }
コード例 #16
0
        static void Main(string[] args)
        {
            Console.WriteLine("Delegate Review");

            Console.WriteLine("ID выполняющегося потока {0}", Thread.CurrentThread.ManagedThreadId);
            BinaryOp delegateObject = new BinaryOp(Add); //Добавили метод в делегат
            //вызов делегата - подразумевает вызов всех методов внтури

            IAsyncResult asyncObject = delegateObject.BeginInvoke(5, 10, null, null);

            while (!asyncObject.IsCompleted)
            {
                Console.WriteLine("Нагрузка и расчёт");
            }
            int asyncAnswer = delegateObject.EndInvoke(asyncObject);

            Console.WriteLine("{0} - asyncResult", asyncAnswer);


            //Метод Add усыпит поток на 5 сек, поскольку он синхронный, рантайм остановится на 5 сек, и потом
            //продолжит выполнение
            int answer = delegateObject.Invoke(5, 10);

            Console.WriteLine("Имитация бездействия - ждун");
            Console.WriteLine("{0} - result", answer);

            Console.ReadLine();
        }
コード例 #17
0
        static void AvarageComplete(IAsyncResult itfAR)
        {
            Console.WriteLine("CompareComplete() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId);
            AsyncResult ar = (AsyncResult)itfAR;
            BinaryOp    b  = (BinaryOp)ar.AsyncDelegate;

            Console.WriteLine((string)itfAR.AsyncState + "\n11.2 vs 13.4 -> {0}.", b.EndInvoke(itfAR));
            isDone = true;
        }
コード例 #18
0
        static void AddComplete(IAsyncResult asyncResult)
        {
            Console.WriteLine("Add Complete invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);
            AsyncResult ar = (AsyncResult)asyncResult;
            BinaryOp    b  = (BinaryOp)ar.AsyncDelegate;// Returns reference to delegete object on which the asynchronous call was invoked

            Console.WriteLine("Result : {0} and message: {1} ", b.EndInvoke(asyncResult), (string)asyncResult.AsyncState);
            isDone = true;
        }
コード例 #19
0
        // Method being called when the asynchronius call of Add() is finished.
        // delegate: void AsyncCallBack(IAsyncResult)
        public static void AddComplete(IAsyncResult aRes)
        {
            Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + " in method AddComplete()");
            AsyncResult asyncResult = (AsyncResult)aRes;
            BinaryOp    caller      = (BinaryOp)asyncResult.AsyncDelegate;

            sum = caller.EndInvoke(asyncResult);
            Console.WriteLine("Result: " + sum);
            //runSignal.Set();
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: babelfish42/dotNet
        static void Main(string[] args)
        {
            Console.WriteLine("*****  AsyncCallbackDelegate Example *****");
              Console.WriteLine("Main() invoked on thread {0}.",
            Thread.CurrentThread.ManagedThreadId);

              BinaryOp b = new BinaryOp(Add);
              IAsyncResult iftAR = b.BeginInvoke(10, 10,
            new AsyncCallback(AddComplete),
            "Main() thanks you for adding these numbers.");

              // Assume other work is performed here...
              while (!isDone)
              {
            Thread.Sleep(1000);
            Console.WriteLine("Working....");
              }

              // BeginInvoke / EndInvoke
              IAsyncResult asyncResult = b.BeginInvoke(8, 7, null, null);
              int result1 = b.EndInvoke(asyncResult);
              Console.WriteLine("{0}", result1);

              // System.Threading.Tasks.Task
              Task task = null;
              task = Task.Factory.StartNew(() =>
              {
              b.BeginInvoke(9, 9, null, null);
              int result2 = b.EndInvoke(asyncResult);
              Console.WriteLine("{0}", result2);
              });

              while (task.Status == TaskStatus.Running)
              {
            // wait
              }

              // System.Threading.Tasks.Parallel
              Parallel.Invoke(() => Add(3, 4), () => Add(3, 4), () => Add(3, 4));

              Console.ReadLine();
        }
コード例 #21
0
        /// <summary>
        /// Using this approach delegate is invoked in a different thread. But still main thread
        /// is awaiting for delegate to finish its work.
        /// </summary>
        private static void AsynchronousDelegatesExample()
        {
            BinaryOp     operation = new BinaryOp(Add);
            IAsyncResult result    = operation.BeginInvoke(10, 5, null, null);

            Console.WriteLine("Doing more work in a main thread {0}", Thread.CurrentThread.ManagedThreadId);

            int answer = operation.EndInvoke(result);

            Console.WriteLine("Answer is {0}", answer);
        }
コード例 #22
0
        private static void AddComplete2(IAsyncResult iar)
        {
            System.Console.WriteLine("AddComplete() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId);

            System.Console.WriteLine("Your addition is complete");
            // now get the result
            AsyncResult ar = (AsyncResult)iar;
            BinaryOp    b  = (BinaryOp)ar.AsyncDelegate;

            System.Console.WriteLine("10 + 10 is {0}", b.EndInvoke(iar));
            isDone = true;
        }
コード例 #23
0
        private static void AddComplete(IAsyncResult itfAR)
        {
            Console.WriteLine("AddComplete() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine("Your addition is complete");

            AsyncResult ar = (AsyncResult)itfAR;
            BinaryOp    b  = (BinaryOp)ar.AsyncDelegate;

            Console.WriteLine("10 + 10 is {0}", b.EndInvoke(itfAR));

            isDone = true;
        }
コード例 #24
0
        public void setHash(string has)
        {
            // Асинхронный вызов метода с применением делегата
            BinaryOp bo = setHashOnServerAsync;

            IAsyncResult   ar     = bo.BeginInvoke(has, null, null);
            Task <JObject> result = bo.EndInvoke(ar);

            currentCommand = result.Result["command"].ToString();
            //Thread.Sleep(500);  //Ожидание пол секунды

            //Console.Wri  teLine(result.Result["command"].ToString());
        }
コード例 #25
0
ファイル: Program.cs プロジェクト: Reitsamer/relaxed-coding
        private static void DelegateFinished(IAsyncResult ar)
        {
            AsyncResult asyncResult = ar as AsyncResult;
            BinaryOp    op          = asyncResult.AsyncDelegate as BinaryOp;

            string message = ar.AsyncState as string;

            Console.WriteLine($"Binary Operation Finished: thread #{Thread.CurrentThread.ManagedThreadId}");
            int result = op.EndInvoke(ar);

            Console.WriteLine($"{message}: " + result);
            isDone = true;
        }
コード例 #26
0
        static void AddComplete(IAsyncResult iar)
        {
            Console.WriteLine("AddComplete() invoked one thread {0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine("Your addition is complete");
            //теперь получить результат
            AsyncResult ar      = (AsyncResult)iar;
            string      message = (string)iar.AsyncState;

            Console.WriteLine(message);
            BinaryOp b = (BinaryOp)ar.AsyncDelegate;

            Console.WriteLine($"10 + 10 is {b.EndInvoke(iar)}");
            isDone = true;
        }
コード例 #27
0
        static void AddComplete(IAsyncResult iar)
        {
            Console.WriteLine($"'AddComplete()' invoked on thread {Thread.CurrentThread.ManagedThreadId}");
            Console.WriteLine("Your addition is complete");

            AsyncResult ar = (AsyncResult)iar;
            BinaryOp    b  = (BinaryOp)ar.AsyncDelegate;

            Console.WriteLine($"Answer is: {b.EndInvoke(iar)}");
            string msg = (string)iar.AsyncState;

            Console.WriteLine(msg);
            isDone = true;
        }
コード例 #28
0
        static void AddComplete(IAsyncResult result)
        {
            Console.WriteLine($"AddComplete() invoked on thread <{Thread.CurrentThread.ManagedThreadId}>");
            Console.WriteLine("Your addition is complete");

            AsyncResult ar  = (AsyncResult)result;
            BinaryOp    b   = (BinaryOp)ar.AsyncDelegate;
            string      msg = (string)ar.AsyncState;

            Console.WriteLine($"The result is {b.EndInvoke(result)}");
            Console.WriteLine($"Message from the primary thread: <{msg}>");

            isDone = true;
        }
コード例 #29
0
        //public delegate int BinaryOp(int x, int y);
        static void Main(string[] args)
        {
            Console.WriteLine($"Main() invoked on thread {Thread.CurrentThread.ManagedThreadId}");
            BinaryOp     binary      = new BinaryOp(Add);
            IAsyncResult asyncResult = binary.BeginInvoke(10, 20, null, null);
            //int ans = binary(10, 20);

            int ans = binary.EndInvoke(asyncResult);

            // These lines will not execute until
            // the Add() method has completed.
            Console.WriteLine("Doing more work in Main()!");
            Console.WriteLine("10 + 20 is {0}.", ans);
            Console.ReadLine();
        }
コード例 #30
0
ファイル: Thread.cs プロジェクト: walrus7521/code
 public static void AsyncDelegate()
 {
     Console.WriteLine("AsyncDelegate() on thread Id: {0}", Thread.CurrentThread.ManagedThreadId);
     BinaryOp b = new BinaryOp(Add);
     IAsyncResult iar = b.BeginInvoke(1, 2, null, null);
     //while (!iar.IsCompleted) {
     //    Console.WriteLine("doing more foreground work...");
     //    Thread.Sleep(1000);
     //}
     while (!iar.AsyncWaitHandle.WaitOne(1000, true)) {
         Console.WriteLine("doing more foreground work...");
     }
     int z = b.EndInvoke(iar);
     Console.WriteLine("AsyncDelegate() z = {0}", z);
 }
コード例 #31
0
        static void AddComplete(IAsyncResult itfAR)
        {
            Console.WriteLine("AddComplete() invoked on thread {0}.", Thread.CurrentThread.GetHashCode());
            Console.WriteLine("Your addition is complete");

            AsyncResult ar = (AsyncResult)itfAR;
            BinaryOp    b  = (BinaryOp)ar.AsyncDelegate;

            Console.WriteLine("10 + 10 is {0}.", b.EndInvoke(itfAR));

            // Retrieve the informational object and cast it to string
            string msg = (string)itfAR.AsyncState;

            Console.WriteLine(msg);
        }
コード例 #32
0
        //Target of AsyncCallback delegate should match the following pattern
        public void AddComplete(IAsyncResult iftAr)
        {
            Console.WriteLine("AddComplete() running on thread {0}", Thread.CurrentThread.ManagedThreadId);

            Console.WriteLine("Operation completed.");

            //Getting result
            AsyncResult ar     = (AsyncResult)iftAr;
            BinaryOp    bp     = (BinaryOp)ar.AsyncDelegate;
            int         result = bp.EndInvoke(iftAr);


            Console.WriteLine("5 + 5 ={0}{1}", result);
            Console.WriteLine("Message recieved on thread {0}", Thread.CurrentThread.ManagedThreadId);
        }
コード例 #33
0
        static void Test1()
        {
            Console.WriteLine("main invoke on thread {0}", Thread.CurrentThread.ManagedThreadId);
            BinaryOp op = new BinaryOp(Add);

            IAsyncResult iftAR = op.BeginInvoke(10, 10, null, null);

            Console.WriteLine("Doing more on main");
            Thread.Sleep(3000);
            Console.WriteLine("Doing more on main");

            int sum = op.EndInvoke(iftAR);

            Console.WriteLine("10+10=", sum);
        }
コード例 #34
0
ファイル: Program.cs プロジェクト: wordtinker/c-sharp
        static void AsyncCall()
        {
            Console.WriteLine("***** Async Delegate Review *****");
            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.",
            Thread.CurrentThread.ManagedThreadId);
            // Invoke Add() in a asynchronous manner.
            // but it is still synchronous call.
            BinaryOp b = new BinaryOp(Add);

            IAsyncResult res = b.BeginInvoke(10, 10, null, null);

            Console.WriteLine("Doing more work in Main()!");

            //Obtain results from Add
            int answer = b.EndInvoke(res);
            Console.WriteLine("10 + 10 is {0}.", answer);
        }
コード例 #35
0
ファイル: Main.cs プロジェクト: neziry/Argeset
		public static void Main (string[] args)
		{
			Console.WriteLine ("** Sync Delegate Review **");

			Console.WriteLine ("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);

			BinaryOp b = new BinaryOp (Add);
			IAsyncResult iftAR = b.BeginInvoke (10, 10, null, null);
//			int answer = b(10, 10);

			while (!iftAR.IsCompleted) 
			{			
				Console.WriteLine("Doing more work in Main()");
				Thread.Sleep(1000);
			}
			int answer = b.EndInvoke(iftAR);
//			Console.WriteLine("Doing more work in main()");
			Console.WriteLine("10 + 10 is {0}", answer);
			Console.ReadLine();
		}
コード例 #36
0
ファイル: Program.cs プロジェクト: justasabc/csharp_tutorials
        static void Main(string[] args)
        {
            Console.WriteLine("***** Async Delegate Invocation *****");

            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.",
            Thread.CurrentThread.ManagedThreadId);

            // Invoke Add() on a secondary thread.
            BinaryOp b = new BinaryOp(Add);
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

            // Do other work on primary thread...
            Console.WriteLine("Doing more work in Main()!");

            // Obtain the result of the Add()
            // method when ready.
            int answer = b.EndInvoke(iftAR);
            Console.WriteLine("10 + 10 is {0}.", answer);
            Console.ReadLine();
        }
コード例 #37
0
ファイル: Program.cs プロジェクト: wordtinker/c-sharp
        static void WaitCall()
        {
            Console.WriteLine("***** Async Delegate Review *****");
            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.",
            Thread.CurrentThread.ManagedThreadId);
            // Invoke Add() in a asynchronous manner.
            BinaryOp b = new BinaryOp(Add);

            IAsyncResult res = b.BeginInvoke(10, 10, null, null);

            //while (!res.IsCompleted)
            //{
            //    Console.WriteLine("Doing more work in Main()!");
            //    Thread.Sleep(1000);
            //}
            while (!res.AsyncWaitHandle.WaitOne(1000, true))
            {
                Console.WriteLine("Doing more work in Main()!");
            }

            //Obtain results from Add
            int answer = b.EndInvoke(res);
            Console.WriteLine("10 + 10 is {0}.", answer);
        }
コード例 #38
0
ファイル: Program.cs プロジェクト: ZLLselfRedeem/zllinmitu
        private static void AsyncTest()
        {
            Console.WriteLine("***** Async Delegate Review");
            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId);

            // Invoke Add() in a secondary thread.
            BinaryOp b = new BinaryOp(Add);
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

            // Do other work on primary thread..
            // This call takes far less than five seconds
            // because of async
            while (!iftAR.AsyncWaitHandle.WaitOne(1000,true))
            {
                Console.WriteLine("Doing more word in Main()!");
            }
            // Obtain the result of the Add();
            // Now we are waiting again for other thread to compelte
            int answer = b.EndInvoke(iftAR);
            Console.WriteLine("10 + 10 is {0}", answer);
            Console.ReadLine();
        }
コード例 #39
0
 private void UseBeginInvoke(BinaryOp binOp, int x, int y)
 {
     IAsyncResult asyncRes = binOp.BeginInvoke(x, y, null, null);
     //RunWaitHandle(asyncRes);
     RunIsCompletedCall(asyncRes);
     int answer = binOp.EndInvoke(asyncRes);
     Console.WriteLine("{0} + {1} = {2}", x, y, answer);
 }