markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / AliveViewModel.cs @ 06f13e11
이력 | 보기 | 이력해설 | 다운로드 (4.29 KB)
1 |
using Markus.Service.WcfClient.StationServiceTask; |
---|---|
2 |
using System; |
3 |
using System.Collections.Generic; |
4 |
using System.ComponentModel; |
5 |
using System.Linq; |
6 |
using System.Text; |
7 |
using System.Threading.Tasks; |
8 |
using Markus.Service.Extensions; |
9 |
|
10 |
|
11 |
namespace Markus.Service.StationController.ViewModel |
12 |
{ |
13 |
public class AliveViewModel :Mvvm.ToolKit.ViewModelBase |
14 |
{ |
15 |
BackgroundWorker backgroundWorker; |
16 |
|
17 |
private List<ConvertItem> aliveItems; |
18 |
private bool isLoading; |
19 |
|
20 |
public List<ConvertItem> AliveItems |
21 |
{ |
22 |
get => aliveItems; set |
23 |
{ |
24 |
aliveItems = value; |
25 |
OnPropertyChanged(() => AliveItems); |
26 |
} |
27 |
} |
28 |
|
29 |
public bool IsLoading |
30 |
{ |
31 |
get => isLoading; set |
32 |
{ |
33 |
if (isLoading != value) |
34 |
{ |
35 |
isLoading = value; |
36 |
OnPropertyChanged(() => IsLoading); |
37 |
} |
38 |
} |
39 |
} |
40 |
|
41 |
public AliveViewModel() |
42 |
{ |
43 |
} |
44 |
|
45 |
// 진행률에 변화가 있을때 이벤트가 발생 |
46 |
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) |
47 |
{ |
48 |
} |
49 |
|
50 |
// 일이 모두 마쳤을때 수행되어야할 코드 |
51 |
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
52 |
{ |
53 |
} |
54 |
|
55 |
// BackgroundWorker에서 수행할 일을 정의. |
56 |
private async void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) |
57 |
{ |
58 |
while (IsAcitve) |
59 |
{ |
60 |
System.Threading.Thread.Sleep(1000); |
61 |
|
62 |
try |
63 |
{ |
64 |
IsLoading = true; |
65 |
|
66 |
List<ConvertItem> newitems = new List<ConvertItem>(); |
67 |
|
68 |
foreach (var client in App.StationClientList) |
69 |
{ |
70 |
if (SimplePing(client.Endpoint.Address.ToString())) |
71 |
{ |
72 |
var items = await client.AliveConvertListAsync(); |
73 |
newitems.AddRange(items); |
74 |
} |
75 |
} |
76 |
|
77 |
newitems.Update(AliveItems); |
78 |
|
79 |
AliveItems = newitems; |
80 |
|
81 |
} |
82 |
catch (Exception ex) |
83 |
{ |
84 |
} |
85 |
} |
86 |
|
87 |
} |
88 |
|
89 |
|
90 |
public static bool SimplePing(string uri) |
91 |
{ |
92 |
bool result = false; |
93 |
|
94 |
try |
95 |
{ |
96 |
using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient()) |
97 |
{ |
98 |
|
99 |
System.Net.Http.HttpResponseMessage responseMessage = Client.GetAsync(uri).Result; |
100 |
System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode; |
101 |
|
102 |
switch (StatusCode) |
103 |
{ |
104 |
|
105 |
case System.Net.HttpStatusCode.Accepted: |
106 |
case System.Net.HttpStatusCode.OK: |
107 |
result = true; |
108 |
break; |
109 |
} |
110 |
} |
111 |
} |
112 |
catch (Exception) |
113 |
{ |
114 |
} |
115 |
|
116 |
return result; |
117 |
} |
118 |
|
119 |
public override void Loaded() |
120 |
{ |
121 |
base.Loaded(); |
122 |
|
123 |
if (!IsDesignMode) |
124 |
{ |
125 |
backgroundWorker = new BackgroundWorker(); |
126 |
backgroundWorker.DoWork += backgroundWorker_DoWork; |
127 |
backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; |
128 |
backgroundWorker.WorkerReportsProgress = false; |
129 |
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
130 |
|
131 |
backgroundWorker.RunWorkerAsync(); |
132 |
} |
133 |
} |
134 |
|
135 |
public override void Closed() |
136 |
{ |
137 |
if (backgroundWorker != null) |
138 |
{ |
139 |
backgroundWorker.DoWork -= backgroundWorker_DoWork; |
140 |
backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted; |
141 |
backgroundWorker.WorkerReportsProgress = false; |
142 |
backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
143 |
|
144 |
backgroundWorker.Dispose(); |
145 |
} |
146 |
|
147 |
base.Closed(); |
148 |
} |
149 |
} |
150 |
} |