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

Invoke() public method

Invokes the delegate on this invoker's thread and returns upon completion
public Invoke ( Delegate method, object args ) : object
method System.Delegate
args object
return object
        public object Invoke(Delegate method, object[] args)
        {
            // we can avoid locking here since the result never leaves this method
            var result = (AsyncResult)this.BeginInvoke(method, args);
            result.OnComplete.WaitOne();

            return result.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!");
        }