■ ProcessDiagnosticInfo 클래스의 GetForProcesses 정적 메소드를 사용해 프로세스 진단 정보 리스트를 구하는 방법을 보여준다.
▶ Package.appxmanifest
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 |
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="mp uap iot rescap"> <Identity Name="1e3306d1-7340-4412-a1b7-e908babd8ddb" Publisher="CN=king" Version="1.0.0.0" /> <mp:PhoneIdentity PhoneProductId="1e3306d1-7340-4412-a1b7-e908babd8ddb" PhonePublisherId="00000000-0000-0000-0000-000000000000" /> <Properties> <DisplayName>TestProject</DisplayName> <PublisherDisplayName>king</PublisherDisplayName> <Logo>Assets\StoreLogo.png</Logo> </Properties> <Dependencies> <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" /> </Dependencies> <Resources> <Resource Language="x-generate" /> </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="TestProject.App"> <uap:VisualElements DisplayName="ProcessDiagnosticInfo 클래스 : GetForProcesses 정적 메소드를 사용해 프로세스 진단 정보 리스트 구하기" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="TestProject" BackgroundColor="transparent"> <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" /> <uap:SplashScreen Image="Assets\SplashScreen.png" /> </uap:VisualElements> </Application> </Applications> <Capabilities> <Capability Name="internetClient" /> <rescap:Capability Name="appDiagnostics" /> </Capabilities> </Package> |
▶ ByteToMegabyteConverter.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 |
using System; using Windows.UI.Xaml.Data; namespace TestProject { /// <summary> /// 바이트→메가바이트 변환자 /// </summary> public class ByteToMegabyteConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValue, targetType, parameter, language) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="language">언어</param> /// <returns>변환 값</returns> public object Convert(object sourceValue, Type targetType, object parameter, string language) { if(sourceValue == null) { return null; } double temporaryValue = (double)((ulong)sourceValue) / (1024 * 1024); string targetValue = $"{temporaryValue:N2} MB"; return targetValue; } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetType, parameter, language) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="language">언어</param> /// <returns>역변환 값</returns> public object ConvertBack(object sourceValue, Type targetType, object parameter, string language) { throw new NotImplementedException(); } #endregion } } |
▶ CPUTimeConverter.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 |
using System; using Windows.UI.Xaml.Data; namespace TestProject { /// <summary> /// CPU 시간 변환자 /// </summary> public class CPUTimeConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValue, targetType, parameter, language) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="language">언어</param> /// <returns>변환 값</returns> public object Convert(object sourceValue, Type targetType, object parameter, string language) { if(sourceValue == null) { return null; } string targetValue = string.Empty; TimeSpan timeSpan = (TimeSpan)sourceValue; if(timeSpan != TimeSpan.MinValue) { targetValue = $"{sourceValue:h\\:mm\\:ss}"; } return targetValue; } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetType, parameter, language) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="language">언어</param> /// <returns>역변환 값</returns> public object ConvertBack(object sourceValue, Type targetType, object parameter, string language) { throw new NotImplementedException(); } #endregion } } |
▶ ProcessModel.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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
using System; using Windows.System.Diagnostics; using Windows.UI.Xaml.Media.Imaging; namespace TestProject { /// <summary> /// 프로세스 모델 /// </summary> public class ProcessModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 프로세스 진단 정보 /// </summary> private ProcessDiagnosticInfo processDiagnosticInfo; /// <summary> /// 프로세스 CPU 사용 리포트 /// </summary> private ProcessCpuUsageReport processCpuUsageReport; /// <summary> /// 프로세스 메모리 사용 리포트 /// </summary> private ProcessMemoryUsageReport processMemoryUsageReport; /// <summary> /// 프로세스 디스크 사용 리포트 /// </summary> private ProcessDiskUsageReport processDiskUsageReport; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 로고 - Logo /// <summary> /// 로고 /// </summary> public BitmapImage Logo { get; set; } #endregion #region 실행 파일명 - ExecutableFileName /// <summary> /// 실행 파일명 /// </summary> public string ExecutableFileName { get { return this.processDiagnosticInfo.ExecutableFileName; } } #endregion #region 프로세스 ID - ProcessID /// <summary> /// 프로세스 ID /// </summary> public uint ProcessID { get { return this.processDiagnosticInfo.ProcessId; } } #endregion #region 프로세스 시작 시간 - ProcessStartTime /// <summary> /// 프로세스 시작 시간 /// </summary> public DateTimeOffset ProcessStartTime { get { return this.processDiagnosticInfo.ProcessStartTime; } } #endregion #region 커널 시간 - KernelTime /// <summary> /// 커널 시간 /// </summary> public TimeSpan KernelTime { get { return this.processCpuUsageReport != null ? this.processCpuUsageReport.KernelTime : TimeSpan.MinValue; } } #endregion #region 사용자 시간 - UserTime /// <summary> /// 사용자 시간 /// </summary> public TimeSpan UserTime { get { return this.processCpuUsageReport != null ? this.processCpuUsageReport.UserTime : TimeSpan.MinValue; } } #endregion #region CPU 시간 - CPUTime /// <summary> /// CPU 시간 /// </summary> public TimeSpan CPUTime { get { return this.processCpuUsageReport != null ? this.processCpuUsageReport.KernelTime + this.processCpuUsageReport.UserTime : TimeSpan.MinValue; } } #endregion #region 페이지 파일 크기 (바이트) - PageFileSize /// <summary> /// 페이지 파일 크기 (바이트) /// </summary> public ulong PageFileSize { get { return this.processMemoryUsageReport != null ? this.processMemoryUsageReport.PageFileSizeInBytes : ulong.MinValue; } } #endregion #region 작업 세트 크기 (바이트) - WorkingSetSize /// <summary> /// 작업 세트 크기 (바이트) /// </summary> public ulong WorkingSetSize { get { return this.processMemoryUsageReport != null ? this.processMemoryUsageReport.WorkingSetSizeInBytes : ulong.MinValue; } } #endregion #region 읽기 바이트 카운트 - ReadByteCount /// <summary> /// 읽기 바이트 카운트 /// </summary> public ulong ReadByteCount { get { return this.processDiskUsageReport != null ? (ulong)this.processDiskUsageReport.BytesReadCount : ulong.MinValue; } } #endregion #region 쓰기 바이트 카운트 - WriteByteCount /// <summary> /// 쓰기 바이트 카운트 /// </summary> public ulong WriteByteCount { get { return this.processDiskUsageReport != null ? (ulong)this.processDiskUsageReport.BytesWrittenCount : ulong.MinValue; } } #endregion #region 디스크 바이트 카운트 - DiskByteCount /// <summary> /// 디스크 바이트 카운트 /// </summary> public ulong DiskByteCount { get { return this.processDiskUsageReport != null ? (ulong)(this.processDiskUsageReport.BytesReadCount + this.processDiskUsageReport.BytesWrittenCount) : ulong.MinValue; } } #endregion #region 앱 타입 - AppType /// <summary> /// 앱 타입 /// </summary> public string AppType { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ProcessModel(processDiagnosticInfo, bitmapImage) /// <summary> /// 생성자 /// </summary> /// <param name="processDiagnosticInfo">프로세스 진단 정보</param> /// <param name="bitmapImage">비트맵 이미지</param> public ProcessModel(ProcessDiagnosticInfo processDiagnosticInfo, BitmapImage bitmapImage) { this.processDiagnosticInfo = processDiagnosticInfo; this.processCpuUsageReport = this.processDiagnosticInfo.CpuUsage.GetReport(); this.processDiskUsageReport = this.processDiagnosticInfo.DiskUsage.GetReport(); this.processMemoryUsageReport = this.processDiagnosticInfo.MemoryUsage.GetReport(); Logo = bitmapImage; AppType = processDiagnosticInfo.IsPackaged ? "Packaged" : "Win32"; } #endregion } } |
▶ MainPage.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 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 |
<Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" xmlns:local="using:TestProject" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Page.Resources> <local:ByteToMegabyteConverter x:Key="ByteToMegabyteConverterKey" /> <local:CPUTimeConverter x:Key="CPUTimeConverterKey" /> </Page.Resources> <Grid> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="10" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <controls:DataGrid Name="dataGrid" Grid.Row="0" BorderBrush="Black" BorderThickness="1" AutoGenerateColumns="False"> <controls:DataGrid.Columns> <controls:DataGridTextColumn Header="명칭" Width="Auto" Binding="{Binding ExecutableFileName}" /> <controls:DataGridTextColumn Header="PID" Width="Auto" Binding="{Binding ProcessID}" /> <controls:DataGridTextColumn Header="커널 시간" Width="Auto" Binding="{Binding KernelTime, Converter={StaticResource CPUTimeConverterKey}}" /> <controls:DataGridTextColumn Header="사용자 시간" Width="Auto" Binding="{Binding UserTime, Converter={StaticResource CPUTimeConverterKey}}" /> <controls:DataGridTextColumn Header="페이지 파일" Width="Auto" Binding="{Binding PageFileSize, Converter={StaticResource ByteToMegabyteConverterKey}}" /> <controls:DataGridTextColumn Header="작업 세트" Width="Auto" Binding="{Binding WorkingSetSize, Converter={StaticResource ByteToMegabyteConverterKey}}" /> <controls:DataGridTextColumn Header="디스크 읽기" Width="Auto" Binding="{Binding ReadByteCount, Converter={StaticResource ByteToMegabyteConverterKey}}" /> <controls:DataGridTextColumn Header="디스크 쓰기" Width="Auto" Binding="{Binding WriteByteCount, Converter={StaticResource ByteToMegabyteConverterKey}}" /> <controls:DataGridTextColumn Header="타입" Width="Auto" Binding="{Binding AppType}" /> </controls:DataGrid.Columns> </controls:DataGrid> <Button Name="refreshButton" Grid.Row="2" HorizontalAlignment="Right" Width="100" Height="30" Content="갱신" /> </Grid> </Grid> </Page> |
▶ MainPage.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 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using Windows.System; using Windows.System.Diagnostics; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 디폴트 프로세스 이미지 /// </summary> private BitmapImage defaultProcessImage; /// <summary> /// 디폴트 앱 이미지 /// </summary> private BitmapImage defaultAppImage; /// <summary> /// 프로세스 컬렉션 /// </summary> private ObservableCollection<ProcessModel> processCollection = new ObservableCollection<ProcessModel>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.defaultProcessImage = new BitmapImage(new Uri("ms-appx:/Assets/default-process-icon.png", UriKind.Absolute)); this.defaultAppImage = new BitmapImage(new Uri("ms-appx:/Assets/default-app-icon.png" , UriKind.Absolute)); this.dataGrid.ItemsSource = this.processCollection; this.refreshButton.Click += refreshButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected async override void OnNavigatedTo(NavigationEventArgs e) { await AppDiagnosticInfo.RequestAccessAsync(); SetProcessData(); base.OnNavigatedTo(e); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 갱신 버튼 클릭시 처리하기 - refreshButton_Click(sender, e) /// <summary> /// 갱신 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void refreshButton_Click(object sender, RoutedEventArgs e) { SetProcessData(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 프로세스 데이터 설정하기 - SetProcessData() /// <summary> /// 프로세스 데이터 설정하기 /// </summary> private void SetProcessData() { this.processCollection.Clear(); IReadOnlyList<ProcessDiagnosticInfo> processDiagnosticInfoList = ProcessDiagnosticInfo.GetForProcesses(); if(processDiagnosticInfoList != null) { foreach(ProcessDiagnosticInfo processDiagnosticInfo in processDiagnosticInfoList) { BitmapImage bitmapImage = null; if(processDiagnosticInfo.IsPackaged) { bitmapImage = this.defaultAppImage; } else { bitmapImage = this.defaultProcessImage; } ProcessModel process = new ProcessModel(processDiagnosticInfo, bitmapImage); this.processCollection.Add(process); } } } #endregion } } |