■ 지연 실행 액션을 사용하는 방법을 보여준다.
▶ DeferredAction.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
using System; using System.Threading; using System.Windows; namespace TestProject { /// <summary> /// 지연 실행 액션 /// </summary> public sealed class DeferredAction : IDisposable { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 타이머 /// </summary> private readonly Timer timer; /// <summary> /// 리소스 해제 여부 /// </summary> private bool isDisposed; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Private #region 생성자 - DeferredAction(action) /// <summary> /// 생성자 /// </summary> /// <param name="action">액션</param> private DeferredAction(Action<DeferredAction> action) { this.timer = new Timer ( state => Application.Current?.Dispatcher?.Invoke(() => action(this)) ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 생성하기 - Create(action) /// <summary> /// 생성하기 /// </summary> /// <param name="action">액션</param> /// <returns>지연 실행 액션</returns> public static DeferredAction Create(Action<DeferredAction> action) { if(action == null) { throw new ArgumentNullException(nameof(action)); } return new DeferredAction(action); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 지연 실행하기 - Defer(deferTime) /// <summary> /// 지연 실행하기 /// </summary> /// <param name="deferTime">지연 시간</param> public void Defer(TimeSpan deferTime) { this.timer.Change(deferTime, Timeout.InfiniteTimeSpan); } #endregion #region 리소스 해제하기 - Dispose() /// <summary> /// 리소스 해제하기 /// </summary> public void Dispose() { if(this.isDisposed) { return; } this.isDisposed = true; this.timer.Dispose(); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="지연 실행 액션 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Button Name="button" HorizontalAlignment="Center" Margin="10" Width="100" Height="30" Content="테스트" /> <ListBox Name="listBox" HorizontalAlignment="Center" Margin="10" Width="300" Height="300" /> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.button.Click += button_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 버튼 클릭시 처리하기 - button_Click(sender, e) /// <summary> /// 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_Click(object sender, RoutedEventArgs e) { this.listBox.Items.Add($"[{DateTime.Now.ToString("HH:mm:ss")}] 버튼을 클릭했습니다."); DeferredAction deferredAction = DeferredAction.Create ( context => { this.listBox.Items.Add($"[{DateTime.Now.ToString("HH:mm:ss")}] 작업을 실행했습니다."); context.Dispose(); } ); deferredAction.Defer(TimeSpan.FromSeconds(3)); } #endregion } } |