Exemplo n.º 1
0
        public void Start()
        {
            Console.WriteLine("Started.");
            DoWorkDelegate d = FindThemClones;

            d.BeginInvoke(CodeText, WhenDoneCallback, d);
        }
Exemplo n.º 2
0
        private void SendFileRun(String filePath)
        {
            DisableStartButtons();
            DoWorkDelegate worker = new DoWorkDelegate(SendFile);

            worker.BeginInvoke(filePath, new AsyncCallback(DoWorkComplete), worker);
        }
Exemplo n.º 3
0
        private IAsyncResult BeginLoadSlowContent(Object src, EventArgs args, AsyncCallback cb, Object state)
        {
            System.Diagnostics.Trace.WriteLine("BeginLoadSlowContent " + Thread.CurrentThread.ManagedThreadId);

            _doWork = LoadProductList;
            return _doWork.BeginInvoke(cb, state);
        }
        public void Demo01()
        {
            Debug.WriteLine($"Current executing thread ID of Demo01 unit test method is: {Thread.CurrentThread.ManagedThreadId.ToString()}");

            // With a delegate instead of calling DoWork directly
            //DoWork();

            // We can do this
            DoWorkDelegate doWorkDelegate = new DoWorkDelegate(DoWork);

            // The delegate keyword behind the scene is generating a whole class
            //doWorkDelegate(); // or doWorkDelegate.Invoke();

            // So doWorkDelegate is actually an object that has methods in it
            // With this we can introduce asynchronous code like the begin/end pattern

            AsyncCallback asyncCallback = new AsyncCallback(TheCallback);

            IAsyncResult ar = doWorkDelegate.BeginInvoke(asyncCallback, doWorkDelegate);

            // do more work
            var result = 1 + 2;

            Debug.WriteLine($"Result is {result}");

            ar.AsyncWaitHandle.WaitOne();
        }
Exemplo n.º 5
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();
        }
        public void CheckDelegateAsyncOperation()
        {
            Debug.WriteLine($"Demo One Thread: {Thread.CurrentThread.ManagedThreadId}");
            var doWork        = new DoWorkDelegate(DoWork);
            var asyncCallback = new AsyncCallback(TheCallBack);
            var asyncResult   = doWork.BeginInvoke(asyncCallback, doWork);

            asyncResult.AsyncWaitHandle.WaitOne();
        }
Exemplo n.º 7
0
        public void Demo01()
        {
            Debug.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
            DoWorkDelegate m = new DoWorkDelegate(DoWork);

            AsyncCallback callback = new AsyncCallback(TheCallback);
            IAsyncResult  ar       = m.BeginInvoke(callback, m);

            // do more

            ar.AsyncWaitHandle.WaitOne();
        }
Exemplo n.º 8
0
        public void Demo1()
        {
            Debug.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
            DoWorkDelegate m        = new DoWorkDelegate(DoWork);
            AsyncCallback  callback = new AsyncCallback(TheCallback);

            // The Begin - End Pattern for Asyncronous Processing
            IAsyncResult ar = m.BeginInvoke(callback, m);

            //Do Work
            ar.AsyncWaitHandle.WaitOne();
        }
Exemplo n.º 9
0
 /// <summary>
 /// Start asyncronous check.
 /// </summary>
 public void Start()
 {
     try
     {
         DoWorkDelegate WorkerThread = new DoWorkDelegate(DoWork);
         WorkerThread.BeginInvoke(m_ComponentIndex, new AsyncCallback(DoWorkComplete), WorkerThread);
     }
     catch (Exception ex)
     {
         OnError(ex, "ASynchCheckBase.Start");
     }
 }
Exemplo n.º 10
0
        public void TestBeginEndPatternWithCallbackAbortException()
        {
            Debug.WriteLine($"Hello from TestBeginEndPatternWithCallbackAbortException() (Thread ID: {Thread.CurrentThread.ManagedThreadId})");
            var doWork = new DoWorkDelegate(DoHardWork);

            var callback = new AsyncCallback(Callback);

            IAsyncResult ar = doWork.BeginInvoke(callback, doWork);

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

            // Will rise ThreadAbortException on the second thread.
        }
Exemplo n.º 11
0
        public void doWorkDelegateAsyncTest()
        {
            Debug.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
            DoWorkDelegate doingwork = new DoWorkDelegate(doWork);

            // BeginInvoke will execute method in different thread
            AsyncCallback asyncCallback = new AsyncCallback(callback);
            IAsyncResult  ar            = doingwork.BeginInvoke(asyncCallback, doingwork);

            // Do work in main thread here

            //doingwork.EndInvoke(ar);
        }
Exemplo n.º 12
0
        public void TestBeginEndPatternWithCallback()
        {
            Debug.WriteLine($"Hello from TestBeginEndPatternWithCallback() (Thread ID: {Thread.CurrentThread.ManagedThreadId})");
            var doWork = new DoWorkDelegate(DoHardWork);

            var callback = new AsyncCallback(Callback);

            IAsyncResult ar = doWork.BeginInvoke(callback, doWork);

            // The callback is made on a ThreadPool thread. ThreadPool threads
            // are background threads, which do not keep the application running
            // if the main thread ends.
            Thread.Sleep(2000);

            Debug.WriteLine($"End of main thread (Thread ID: {Thread.CurrentThread.ManagedThreadId})");
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
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})");
        }
Exemplo n.º 15
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})");
        }
Exemplo n.º 16
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})");
        }