첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
0.03MB

▶ Package.appxmanifest

<?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>

 

728x90

 

▶ ByteToMegabyteConverter.cs

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
    }
}

 

300x250

 

▶ CPUTimeConverter.cs

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

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

<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

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
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요