WPF—延迟执行

作者:追风剑情 发布于:2019-8-19 22:43 分类:C#

示例: 参考https://www.cnblogs.com/qldsrx/archive/2013/02/22/2922682.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;//Dispatcher

namespace WpfApp6
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Click");
            btn2.Visibility = Visibility.Hidden;
            DoWork(ShowButton2, 5000);
        }

        private void ShowButton2()
        {
            Console.WriteLine("Click-Delay");
            //显示btn2
            btn2.Visibility = Visibility.Visible;
        }

        public static void DoWork(Action action, int millisecond = 300)
        {
            new Action<Dispatcher, Action, int>(DoWorkAsync).BeginInvoke(Dispatcher.CurrentDispatcher, action, millisecond, null, null);
        }

        static void DoWorkAsync(Dispatcher dispatcher, Action action, int millisecond)
        {
            System.Threading.Thread.Sleep(millisecond);
            dispatcher.BeginInvoke(action);
        }
    }
}

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号