c#

[c#] timer

초코렛과자 2016. 3. 14. 21:09

c# 에서 timer는 크게 3종류로 나뉜다는데, 이번에 사용한건 System.Threading.Timer 이다. 

아직도 Thread를 잘 이해하지 못하고 있지만, 여러가지 동작은 병렬로 수행하려면 Thread를 사용해야 할 것 같아서 이를 사용했다. 


class timerTest{

delegate void TimerEventFiredDelegate();


System.Threading.Timer timer;


static void main(string[] args)

{

timer = new System.Threading.Timer(Callback);

timer.Change(0, 1000) //dueTime 은 Timer가 시작되기 전 대기 시간(ms)

}


void Callback(obejct status)

{

BeginInvoke(new TimerEventFiredDelegate(Work));

}


private void Work()

{

//timer로 작업할 내용 

}

}




UI에서 사용할 경우는 Cross-Thread 문제가 발생하므로 Invoke 또는 BeginInvoke 를 사용해서 마샬링(?)을 통한 호출을 처리하여야 한다. 


라는 설명이 있는데 아직 잘 모르겠다.