728x90
반응형
728x170
■ DataGrid 클래스에서 데이터 업데이트 성능을 측정하는 방법을 보여준다.
▶ SampleItem.cs
using System.ComponentModel;
namespace TestProject
{
/// <summary>
/// 샘플 항목
/// </summary>
public class SampleItem : INotifyPropertyChanged
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 속성 변경시 이벤트 - PropertyChanged
/// <summary>
/// 속성 변경시 이벤트
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 매도
/// <summary>
private int selling;
/// <summary>
/// 값
/// <summary>
private int value;
/// <summary>
/// 매수
/// <summary>
private int buying;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 매도 - Selling
/// <summary>
/// 매도
/// </summary>
public int Selling
{
get
{
return this.selling;
}
set
{
if(this.selling == value)
{
return;
}
this.selling = value;
FirePropertyChangedEvent(nameof(Selling));
}
}
#endregion
#region 값 - Value
/// <summary>
/// 값
/// </summary>
public int Value
{
get
{
return this.value;
}
set
{
if(this.value == value)
{
return;
}
this.value = value;
FirePropertyChangedEvent(nameof(Value));
}
}
#endregion
#region 매수 - Buying
/// <summary>
/// 매수
/// </summary>
public int Buying
{
get
{
return this.buying;
}
set
{
if(this.buying == value)
{
return;
}
this.buying = value;
FirePropertyChangedEvent(nameof(Buying));
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName)
/// <summary>
/// 속성 변경시 이벤트 발생시키기
/// </summary>
/// <param name="propertyName">속성명</param>
protected void FirePropertyChangedEvent(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="DataGrid 클래스 : 데이터 업데이트 성능 측정하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DataGrid Name="dataGrid" Grid.Row="0"
Margin="10 10 10 0"
AutoGenerateColumns="False"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="매도"
Width="150"
Binding="{Binding Selling}" />
<DataGridTextColumn Header="값"
Width="150"
Binding="{Binding Value}" />
<DataGridTextColumn Header="매수"
Width="150"
Binding="{Binding Buying}" />
</DataGrid.Columns>
</DataGrid>
<Button Name="startButton" Grid.Row="1"
Margin="0 10 10 10"
HorizontalAlignment="Right"
Width="100"
Height="30"
Content="시작" />
<TextBlock Name="timeTextBlock" Grid.Row="1"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="10 10 0 0" />
<TextBlock Name="countTextBlock" Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</Window>
▶ MainWindow.xaml.cs
using System;
using System.Windows;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Threading;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 딕셔너리
/// </summary>
private SortedDictionary<int, SampleItem> dictionary;
/// <summary>
/// 난수기
/// </summary>
private Random random;
/// <summary>
/// 시작 값
/// </summary>
private const int startValue = 1000;
/// <summary>
/// 종료 값
/// </summary>
private const int endValue = 1050;
/// <summary>
/// 이전 값 배열
/// </summary>
private int[] previousValueArray = null;
/// <summary>
/// 타이머
/// </summary>
DispatcherTimer timer = new DispatcherTimer();
/// <summary>
/// 카운트
/// </summary>
private int count = 0;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
this.dictionary = new SortedDictionary<int, SampleItem>();
for(int i = startValue; i < endValue; i++)
{
this.dictionary.Add(i, new SampleItem { Selling = 0, Value = i, Buying = 0 });
}
this.random = new Random(DateTime.Now.Millisecond);
this.dataGrid.ItemsSource = this.dictionary.Values;
this.timer = new DispatcherTimer();
this.timer.Interval = TimeSpan.FromMilliseconds(0.000001);
this.timer.Tick += timer_Tick;
this.startButton.Click += startButton_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 타이머 틱 처리하기 - timer_Tick(sender, e)
/// <summary>
/// 타이머 틱 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void timer_Tick(object sender, EventArgs e)
{
this.countTextBlock.Text = this.count.ToString();
Stopwatch watch = new Stopwatch();
watch.Start();
for(int i = 0; i < 10; i++)
{
count++;
if(this.previousValueArray != null)
{
for(int j = 0; j < 40; j++)
{
int value = this.previousValueArray[j];
SampleItem item = this.dictionary[value];
item.Selling = 0;
item.Buying = 0;
}
}
int[] valueArray = GetRandomValueArray();
for(int j = 0; j < 40; j++)
{
int value = valueArray[j];
SampleItem item = this.dictionary[value];
item.Selling = this.random.Next(1, 100);
item.Buying = this.random.Next(1, 100);
}
this.previousValueArray = valueArray;
}
watch.Stop();
this.timeTextBlock.Text = watch.Elapsed.ToString();
}
#endregion
#region 시작 버튼 클릭시 처리하기 - testButton_Click(sender, e)
/// <summary>
/// 시작 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void startButton_Click(object sender, RoutedEventArgs e)
{
if(this.timer.IsEnabled)
{
this.timer.Stop();
this.startButton.Content = "시작";
}
else
{
this.count = 0;
this.timer.Start();
this.startButton.Content = "중단";
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 임의 값 배열 구하기 - GetRandomValueArray()
/// <summary>
/// 임의 값 배열 구하기
/// </summary>
/// <returns>임의 값 배열</returns>
private int[] GetRandomValueArray()
{
int value = this.random.Next(startValue, 1011);
int[] valueArray = new int[50];
for(int i = 0; i < 40; i++)
{
valueArray[i] = value + i;
}
return valueArray;
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Application 클래스 : GetResourceStream 정적 메소드를 사용해 리소스 XAML 페이지 로드하기 (0) | 2022.01.02 |
---|---|
[C#/WPF] VirtualizingStackPanel 클래스 : IsVirtualizing/VirtualizationMode 속성을 사용해 대용량 데이터 바인딩하기 (0) | 2021.12.31 |
[C#/WPF] Popup 클래스 : 다른 엘리먼트 위에 엘리먼트를 오버레이 배치하기 (0) | 2021.12.31 |
[C#/WPF] HwndHost 클래스 : WPF 윈도우 내부에서 메모장 윈도우 호스팅하기 (0) | 2021.12.31 |
[C#/WPF] 3D 애니메이션 시계 사용하기 (절전 모드 방지 기능 추가) (0) | 2021.11.17 |
[C#/WPF] DataGrid 클래스 : 대용량 데이터 바인딩하기 (0) | 2021.10.03 |
[C#/WPF/.NETCORE] FaceDetector 클래스 : DetectFacesAsync 메소드를 사용해 얼굴 탐지하기 (0) | 2021.09.23 |
[C#/WPF/.NETCORE] 누겟 설치 : Microsoft.Windows.SDK.Contracts (0) | 2021.09.23 |
[C#/WPF/.NETCORE] BitmapImage 클래스 : WINFORM Bitmap 객체에서 비트맵 이미지 구하기 (0) | 2021.09.23 |
[C#/WPF/.NETCORE] IValueConverter 인터페이스 : 비트맵→비트맵 소스 변환자 사용하기 (0) | 2021.09.23 |
댓글을 달아 주세요