본문 바로가기

Code

(117)
Generic Method 를 이용한 Serialization Generic Method 를 사용하면 상당히 유용한 Serialization 을 구사할 수 있다. 아래 예제는 Generic Method 를 사용하여 어떠한 Type이라도 쉽게 Serialize & Deserialize 할 수 있도록 한 것이다. 이런 것들은 실전에서 유용하게 이용될 수 있다. using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace GenericSerializer { public static class Serializer { public static void Export(string otherFile, T otherT) { using (Stream s = File.Open(..
Serializable Attribute & BinaryFormatter 사용법 객체를 serialize 하여 파일에 저장하는 방법은 아래와 같다. serialize 하려는 클래스에 [Serializable] Attribute 를 붙인다. Stream 으로 serialize 파일을 연다. BinaryFormatter.Serialize 로 serialize 된 객체를 파일에 저장한다. BinaryFormatter.Deserialize 를 사용하면 serialize 되어 파일에 저장된 객체를 불러올 수 있다. 아래 예제를 살펴보자. using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace SerializableAttribute0001 { class Program { stati..
간단한 DataGridView 사용 예제 DataGridView 사용에 대한 간단한 예제이다. DataGridView.ColumnCount 를 사용해서 열의 갯수를 정한다. DataGridView.Columns[].Name 으로 각 열의 이름을 정한다. DataGridView.Rows.Add() 를 이용해서 행을 추가한다. using System; using System.Windows.Forms; namespace DataGridViewSample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.dataGridView1.ColumnCount ..
DateTime & TimeSpan : 두 시간 사이의 간격 계산하기 두 시간 사이의 간격을 계산하기 위해서 DateTime 과 TimeSpan 을 사용할 수 있다. DateTime 은 측정하려는 두 시간을 구할 때 사용한다. TimeSpan 은 두 시간 사이의 간격을 계산할 때 사용한다. 아래 예제를 보자. using System; using System.Threading; namespace ThreadTest { class Program { static void Main(string[] args) { int result; int signature; TimeSpan oneSecond = TimeSpan.FromSeconds(1); Console.WriteLine(oneSecond); for (int i = 0; i < 10; i++) { DateTime old = Dat..
Thread.Sleep() & TimeSpan.FromSeconds() Thread.Sleep() 을 사용하면 일정 시간 동안 Thread를 멈출 수 있다. TimeSpan.FromSeconds() 을 사용하면 편하게 milli second 계산을 할 수 있다. 아래 예제를 살펴보자. using System; using System.Threading; namespace ThreadTest { class Program { static object locker = new object(); static void Main(string[] args) { new Thread(Go).Start(); for (int i = 0; i < 20; i++) { Console.Write("."); Thread.Sleep(TimeSpan.FromSeconds(1)); } } static void ..
Thread 에서 locker의 사용 thread의 실행을 컨트롤하기 위해서 locker를 사용할 수 있다. locker는 오로지 lock을 하는 용도로만 쓰인다. 아래 예제를 살펴보자. using System; using System.Threading; namespace ThreadTest { class Program { static object locker = new object(); static void Main(string[] args) { new Thread(Go).Start(); lock (locker) { for (int i = 0; i < 500; i++) { Console.Write("."); } } } static void Go() { lock (locker) { for (int i = 0; i < 500; i++) { C..
static field를 사용한 Thread static field는 모든 Thread에서 공동으로 사용된다. 아래 예제를 살펴보자. using System; using System.Threading; namespace ThreadTest { class Program { // Static fields are shared between all threads static int count; static void Main(string[] args) { new Thread(Go).Start(); Go(); } static void Go() { for (int i = 0; i < 5; i++) { count++; Console.WriteLine(count); } } } } 위의 예제에서 count는 static field이다. 두개의 thread가 coun..
Thread에서 공동의 인스턴스 사용 예제 아래는 두개의 Thread가 공동의 instance를 사용한 예제이다. Foo 클래스 인스턴스인 foo를 공동으로 사용한 두개의 thread가 실행되었다. using System; using System.Threading; namespace ThreadTest { class Program { static void Main(string[] args) { Foo foo = new Foo(); new Thread(foo.Go).Start(); foo.Go(); } } class Foo { private int count = 0; public void Go() { for (int i = 0; i < 5; i++) { count++; Console.WriteLine(count); } } } } 실행 결과 화면은 ..