'csharp'에 해당되는 글 3건

  1. 2008.11.26 [C#] 화면 깜빡임 처리 - 더블버퍼링 6
  2. 2008.11.18 [C#] 특정 내용이 있는 파일 검색하기
  3. 2008.11.18 [C#] 실행시간 측정
2008. 11. 26. 14:18

[C#] 화면 깜빡임 처리 - 더블버퍼링

반응형
어플리케이션 프로그래밍시 화면에 그림을 그릴때 화면에 이미지를 하나하나 그리면 그려지는 과정이 화면에 나타나서 깜빡 거리는 현상이 발생합니다.
그 때문에 미리 다른곳에 그린 후 그려진 결과를 화면에 뿌리는 방법을 사용하게 되는데 이를 "더블버퍼링"기법이라고 하죠.

C#에서는 이게 정말 간단히 구현이 되더군요.
폼이 로드 될 때 (ex public Form1() {})

this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);

를 넣어주면 됩니다

this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

이렇게 해도 됩니다.

ps.
Paint 이벤트 함수 호출 함수는

Invalidate();

입니다.
2008. 11. 18. 13:19

[C#] 특정 내용이 있는 파일 검색하기

반응형
요즘 C#을 간간히 사용해보는 중인데 정말 간단하게 구현이 되는게 많네요.
간단한 유틸은 C#으로 만드는게 상당히 유용한거 같습니다.

using System.IO;

bool bExist = false;
string sPath = @"C:\text.txt";

string sContents = File.ReadAllText(sPath, Encoding.Default);

int result = sContents.IndexOf("검색어");

if (result != -1)
    bExist = true;

그냥 File클래스의 ReadAllText하고 거기 내용 검색 하면 끝.
처음에는 StreamReader로 읽어서 ReadLine()으로 한줄한줄 읽어 와서 검색했었는데
ReadAllText를 이용 하니 365개의 파일 검색하는게 이전방식(StreamReader -> ReadLine -> Contains)으로 8초 정도 걸리던게 지금(File.ReadAllText -> IndexOf)은 600ms 정도 걸리네요.

추가.
StreamReader로도 좀 더 개선할 수 있더군요.
StreamReader sr = new StreamReader(fi.FullName, Encoding.Default);
string sContents = sr.ReadToEnd();
ReadLine은 한줄단위고 그냥 한꺼번에 읽어 올 수 있는 ReadToEnd함수가 있군요.
2008. 11. 18. 13:05

[C#] 실행시간 측정

반응형
C#에서 실행시간 측정하는 방법은 아주아주 간단합니다.

using System.Diagnostics;

Stopwatch sw = new Stopwatch();
sw.Start();
// 처리 프로세스
// while, for, foreach 등
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString() + "ms");


우선 using System.Diagnostics 를 선언해 주시고
Stopwatch 를 사용해 주시면 됩니다.

.Net 2.0부터 지원한다고 합니다. 하지만 이전버전에서도 손쉽게 만들어 쓸수 있는거 같더군요.