WPF—延迟执行

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

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Windows.Threading;//Dispatcher
  15.  
  16. namespace WpfApp6
  17. {
  18. /// <summary>
  19. /// MainWindow.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. }
  27.  
  28. private void Button_Click(object sender, RoutedEventArgs e)
  29. {
  30. Console.WriteLine("Click");
  31. btn2.Visibility = Visibility.Hidden;
  32. DoWork(ShowButton2, 5000);
  33. }
  34.  
  35. private void ShowButton2()
  36. {
  37. Console.WriteLine("Click-Delay");
  38. //显示btn2
  39. btn2.Visibility = Visibility.Visible;
  40. }
  41.  
  42. public static void DoWork(Action action, int millisecond = 300)
  43. {
  44. new Action<Dispatcher, Action, int>(DoWorkAsync).BeginInvoke(Dispatcher.CurrentDispatcher, action, millisecond, null, null);
  45. }
  46.  
  47. static void DoWorkAsync(Dispatcher dispatcher, Action action, int millisecond)
  48. {
  49. System.Threading.Thread.Sleep(millisecond);
  50. dispatcher.BeginInvoke(action);
  51. }
  52. }
  53. }

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号