프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / ID2.Manager.Compare / Classes / BaseWorker.cs @ 4142eefa

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

1 13a36357 humkyung
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
7
using System.ComponentModel;
8
9
using Telerik.WinControls.UI;
10
11
namespace ID2.Manager.Classes
12
{
13
    public class BaseWorker
14
    {
15
        protected System.Windows.Forms.Control _parent = null;
16
        private BackgroundWorker worker = new BackgroundWorker() { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
17
        private RadWaitingBar waiting = null;
18
19
        public BaseWorker(System.Windows.Forms.Control control = null)
20
        {
21
            this._parent = control;
22
        }
23
24
        public virtual void StartWork()
25
        {
26
            if (_parent != null)
27
            {
28
                _parent.Invoke(new Action(() =>
29
                {
30
                    waiting = new RadWaitingBar()
31
                    {
32
                        Size = new System.Drawing.Size(200, 200),
33
                        WaitingStyle = Telerik.WinControls.Enumerations.WaitingBarStyles.DotsSpinner,
34
                        BackColor = System.Drawing.Color.Transparent,
35
                        Parent = _parent
36
                    };
37
                    waiting.Location = new System.Drawing.Point(
38
                        _parent.Location.X + (_parent.Width - waiting.Width) / 2,
39
                        _parent.Location.Y + (_parent.Height - waiting.Height) / 2);
40
                    _parent.Controls.Add(waiting);
41
                }));
42
            }
43
44
            worker.DoWork += Worker_DoWork;
45
            worker.ProgressChanged += Worker_ProgressChanged;
46
            worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
47
            if (!worker.IsBusy)
48
            {
49
                if (this.waiting != null) this.waiting.StartWaiting();
50
                worker.RunWorkerAsync();
51
            }
52
        }
53
54
        public virtual void CancelWork()
55
        {
56
            this.worker.CancelAsync();
57
        }
58
59
        private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
60
        {
61
            this.UpdateProgress(e.ProgressPercentage);
62
        }
63
64
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
65
        {
66
            if (this.waiting != null)
67
            {
68
                try
69
                {
70
                    this.waiting.StopWaiting();
71
                    if (this._parent != null)
72
                    {
73
                        this._parent.Invoke(new Action(() => this._parent.Controls.Remove(this.waiting)));
74
75
                    }
76
                    this.waiting.Dispose();
77
                }
78
                catch (Exception ex)
79
                {
80
                    Console.WriteLine(ex.Message);
81
                }
82
            }
83
            this.WorkCompleted(e);
84
        }
85
86
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
87
        {
88
            this.DoWork(sender as BackgroundWorker, e);
89
        }
90
91
        protected virtual void DoWork(System.ComponentModel.BackgroundWorker worker, DoWorkEventArgs args) { throw new NotImplementedException(); }
92
        protected virtual void UpdateProgress(int precent) { }
93
        protected virtual void WorkCompleted(RunWorkerCompletedEventArgs args) { }
94
    }
95
}
클립보드 이미지 추가 (최대 크기: 500 MB)