BeginInvoke與Invoke 實測

  • 1324
  • 0

摘要:BeginInvoke與Invoke 實測

        private void Run1()
        {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>(ShowString), "Run1...1");
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>(ShowString), "Run1...2");
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>(ShowString), "Run1...3");
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>ShowString), "Run1...4");
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>(ShowString), "Run1...5");
        }
        private void  ShowString(string obj)
        {
            Console.WriteLine(obj);
        }

result :
Run1...3
Run1...5
Run1...1
Run1...2
Run1...4

==================================================================================
        private void Run2()
        {

            System.Action action1 = () =>
            {
                Console.WriteLine("Run2...1");
            };
            System.Action action2 = () =>
            {
                Console.WriteLine("Run2...2");
            };
            System.Action action3 = () =>
            {
                Console.WriteLine("Run2...3");
            };
            System.Action action4 = () =>
            {
                Console.WriteLine("Run2...4");
            };
            System.Action action5 = () =>
            {
                Console.WriteLine("Run2...5");
            };
            this.Dispatcher.BeginInvoke(action1, null);
            this.Dispatcher.BeginInvoke(action2, null);
            this.Dispatcher.Invoke(action3, null);
            this.Dispatcher.BeginInvoke(action4, null);
            this.Dispatcher.Invoke(action5, null);
     
        }
result :
Run2...1
Run2...2
Run2...3
Run2...4
Run2...5

==========================================================================================
        private void Run3()
        {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>(ShowString), "Run3...1");
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>(ShowString), "Run3...2");
            System.Action action = () =>
            {
                Console.WriteLine("Run3...6");
            };
            this.Dispatcher.Invoke(action, null);
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>(ShowString), "Run3...3");
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>ShowString), "Run3...4");
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action<string>(ShowString), "Run3...5");
        }
        private void  ShowString(string obj)
        {
            Console.WriteLine(obj);
        }

result :
Run3...1
Run3...2
Run3...6
Run3...3
Run3...5
Run3...4

 

========結論=========

 this.Dispatcher.Invoke(action, null);有將目前有的BeginInvoke全部做完以及執行自己的action的效果