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

BeginInvoke() public method

Queue the delegate to be invoked on this invoker's thread
public BeginInvoke ( Delegate method, object args ) : IAsyncResult
method System.Delegate
args object
return IAsyncResult
        public IAsyncResult BeginInvoke(Delegate method, object[] args)
        {
            var result = new AsyncResult() { OnComplete = this.GetMRE(), CompletedSynchronously = !this.InvokeRequired };
            this.queue.Enqueue(Tuples.New(method, args, result));

            return result;
        }

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