System.Threading.SingleThreadedInvoker.EndInvoke C# (CSharp) Method

EndInvoke() public method

Returns when the asynchronous invokation represented by result completes
public EndInvoke ( IAsyncResult result ) : object
result IAsyncResult
return object
        public object EndInvoke(IAsyncResult result)
        {
            var asyncResult = result as AsyncResult;
            if (asyncResult == null)
                throw new Exception("Result was not created by this ISynchronizedInvoke!");

            lock (asyncResult)
            {
                if (asyncResult.endInvokedCalled)
                    throw new Exception("EndInvoke called twice on result!");
                asyncResult.endInvokedCalled = true;
            }

            asyncResult.OnComplete.WaitOne();

            return asyncResult.AsyncState;
        }

Usage Example

示例#1
0
        public static void TestInvoker()
        {
            var range = Arrays.Range(0, 1, 100);
            var list = new List<int>();
            IAsyncResult result = null;

            using (var sti = new SingleThreadedInvoker())
            {
                foreach (int i in range)
                {
                    int j = i;
                    if (j % 2 == 0)
                        result = sti.BeginInvoke(new Action(() =>
                        {
                            list.Add(j);
                        }), new object[0]);
                    else
                        sti.Invoke(new Action(() =>
                        {
                            list.Add(j);
                        }), new object[0]);
                }

                // wait for all to finish
                sti.EndInvoke(result);
            }

            if (!list.SequenceEqual(range))
                throw new Exception("SingleThreadedInvoker failed!");
        }