Beispiel #1
0
        private void DoWorkComplete(IAsyncResult workID)
        {
            EnableStartButtons();
            DoWorkDelegate worker = workID.AsyncState as DoWorkDelegate;

            worker.EndInvoke(workID);
        }
Beispiel #2
0
        private void WhenDoneCallback(IAsyncResult ar)
        {
            DoWorkDelegate d          = (DoWorkDelegate)ar.AsyncState;
            ScanResult     scanResult = d.EndInvoke(ar);

            InvokeJobDone(scanResult);
        }
Beispiel #3
0
        static void Main()
        {
            DoWorkDelegate del = DoWork;

            // Note: У вас есть два способа получить уведомление об окончании метода:

            // Note: 1) Через метод обратного вызова

            IAsyncResult asyncResult = del.BeginInvoke(100000000,
                                                       stateObject => Console.WriteLine("Computation done"), // Фактически никакое состояние передано не было
                                                       null);

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Doing other work...{0}", i);
                Thread.Sleep(1000);
            }

            // Note: 2) С помощью EndInvoke

            double sum = del.EndInvoke(asyncResult); // Дождаться окончания

            Console.WriteLine("Sum: {0}", sum);

            Console.ReadKey();
        }
Beispiel #4
0
        public static void TestCallMethodWithoutWaitingForReturn()
        {
            DoWorkDelegate del = DoWork;
            //two ways to be notified of when method ends:
            // 1. callback method
            // 2. call EndInvoke
            IAsyncResult res = del.BeginInvoke(100000000, DoWorkDone, null);

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Doing other work...{0}", i);
                Thread.Sleep(1000);
            }
            //wait for end
            double sum = del.EndInvoke(res);

            Console.WriteLine("Sum: {0}", sum);
        }
Beispiel #5
0
        public void TestBeginEndPatternWithWaitHandle()
        {
            Debug.WriteLine($"Hello from TestSimpleBeginEndPattern() (Thread ID: {Thread.CurrentThread.ManagedThreadId})");

            // Create the delegate.
            var caller = new DoWorkDelegate(DoHardWork);

            IAsyncResult result = caller.BeginInvoke(null, null);

            // Wait for the WaitHandle to become signaled.
            result.AsyncWaitHandle.WaitOne();

            caller.EndInvoke(result);

            // Close the wait handle.
            result.AsyncWaitHandle.Close();

            Debug.WriteLine($"End of main thread (Thread ID: {Thread.CurrentThread.ManagedThreadId})");
        }
Beispiel #6
0
        public void TestBeginEndPatternWithPolling()
        {
            Debug.WriteLine($"Hello from TestBeginEndPatternWithPolling() (Thread ID: {Thread.CurrentThread.ManagedThreadId})");

            // Create the delegate.
            var caller = new DoWorkDelegate(DoHardWork);

            IAsyncResult result = caller.BeginInvoke(null, null);

            // Poll while simulating work.
            while (result.IsCompleted == false)
            {
                Debug.WriteLine($"Hello again from TestBeginEndPatternWithPolling() (Thread ID: {Thread.CurrentThread.ManagedThreadId})");

                Thread.Sleep(250); // Attention! Bad code
            }

            caller.EndInvoke(result);

            Debug.WriteLine($"End of main thread (Thread ID: {Thread.CurrentThread.ManagedThreadId})");
        }
Beispiel #7
0
        public void TestBeginEndPatternSimple()
        {
            Debug.WriteLine($"Hello from TestSimpleBeginEndPattern() (Thread ID: {Thread.CurrentThread.ManagedThreadId})");

            // Create the delegate.
            var caller = new DoWorkDelegate(DoSimpleWork);

            // The BeginInvoke method initiates the asynchronous call.
            // BeginInvoke returns an IAsyncResult, which can be used to monitor the progress of the asynchronous call.
            IAsyncResult result = caller.BeginInvoke(null, null);

            // Simulating some work in the main thread.
            Thread.Sleep(0);
            Debug.WriteLine($"Hello again from TestSimpleBeginEndPattern() (Thread ID: {Thread.CurrentThread.ManagedThreadId})");

            // The EndInvoke method retrieves the results of the asynchronous call.
            // It can be called any time after BeginInvoke.
            // If the asynchronous call has not completed, EndInvoke blocks the calling thread until it completes.
            caller.EndInvoke(result);

            Debug.WriteLine($"End of main thread (Thread ID: {Thread.CurrentThread.ManagedThreadId})");
        }