프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkusAutoUpdate / src / NetSparkle.UI.Avalonia / Controls / BaseWindow.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (2.36 KB)

1 d8f5045e taeseongkim
using Avalonia.Controls;
2
using Avalonia.Media.Imaging;
3
using Avalonia.Threading;
4
using System;
5
using System.Collections.Generic;
6
using System.Text;
7
using System.Threading;
8
using System.Windows;
9
10
namespace NetSparkleUpdater.UI.Avalonia.Controls
11
{
12
    public class BaseWindow : Window
13
    {
14
        protected bool _isOnMainThread;
15
        protected bool _hasInitiatedShutdown;
16
17
        protected CancellationTokenSource _cancellationTokenSource;
18
19
        public BaseWindow()
20
        {
21
            _cancellationTokenSource = new CancellationTokenSource();
22
            _hasInitiatedShutdown = false;
23
        }
24
25
        public BaseWindow(bool useClosingEvent)
26
        {
27
            if (useClosingEvent)
28
            {
29
                Closing += BaseWindow_Closing;
30
            }
31
            _cancellationTokenSource = new CancellationTokenSource();
32
            _hasInitiatedShutdown = false;
33
        }
34
35
        private void BaseWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
36
        {
37
            Closing -= BaseWindow_Closing;
38
            if (!_isOnMainThread && !_hasInitiatedShutdown)
39
            {
40
                _hasInitiatedShutdown = true;
41
                _cancellationTokenSource.Cancel();
42
            }
43
        }
44
45
        protected void ShowWindow(bool isOnMainThread)
46
        {
47
48
            try
49
            {
50
                Show();
51
                _isOnMainThread = isOnMainThread;
52
                if (!isOnMainThread)
53
                {
54
                    Dispatcher.UIThread.MainLoop(_cancellationTokenSource.Token);
55
                }
56
            }
57
            catch (ThreadAbortException)
58
            {
59
                Close();
60
                if (!isOnMainThread)
61
                {
62
                    _cancellationTokenSource.Cancel();
63
                }
64
            }
65
        }
66
67
        protected void CloseWindow()
68
        {
69
            // make sure to close the window on the thread it has been started on
70
            Dispatcher.UIThread.InvokeAsync(() =>
71
            {
72
                Close();
73
                if (!_isOnMainThread && !_hasInitiatedShutdown)
74
                {
75
                    _hasInitiatedShutdown = true;
76
                    _cancellationTokenSource.Cancel();
77
                }
78
            });
79
        }
80
81
        protected void BringToFront()
82
        {
83
            Topmost = true;
84
            Activate();
85
            Topmost = false;
86
        }
87
    }
88
}
클립보드 이미지 추가 (최대 크기: 500 MB)