Thread.Timers에 대한 MSDN 예제 (http://msdn.microsoft.com/en-us/library/swx5easy.aspx)를 자체 코드로 구현하려고합니다.다른 클래스의 스레드 타이머 취소
특정 사용자 작업이 수행 될 때 타이머를 취소 할 수 있기를 원하지만 타이머를 처리 할 수 없습니다. 다른 클래스의 메서드를 호출하므로 조정할 필요가 있습니다. 그러나 나는 어디 있는지 모른다.
이외의 타이머가 정상적으로 작동합니다. btnconfigOpenConfig가 호출 될 때 왜 내 타이머가 취소되지 않는지 알 수 있습니까?
FYI 저는 작업자 프로세스를 시간 초과 이벤트로 변환합니다. 한스는 의견에서 지적는
public partial class Xservt : Window
{
internal class TimerStateObjClass
{
public int SomeValue;
public System.Threading.Timer SqlUpdateFromTwitterTimerReference;
public bool TimerCanceled;
}
internal void SomeMethod(){
TimerStateObjClass stateObj = new TimerStateObjClass();
stateObj.TimerCanceled = false;
stateObj.SomeValue = 100;
System.Threading.TimerCallback timerDelegate =
new System.Threading.TimerCallback(twit.hometimelineclass._sqlUpdateFromTwitterWorker_DoWork);
var sqlUpdateFromTwitterTimer = new Timer(timerDelegate, stateObj, 0,20000);
stateObj.SqlUpdateFromTwitterTimerReference = sqlUpdateFromTwitterTimer;
}
}
//action to perform which disposes the timer
private void btnconfigOpenConfig(object sender, RoutedEventArgs e)
{
TimerStateObjClass timerState = new TimerStateObjClass();
timerState.TimerCanceled = true;
}
//Actions the timer is calling, in another class
internal static void _sqlUpdateFromTwitterWorker_DoWork(object StateObj)
{
Xservt.TimerStateObjClass state = (Xservt.TimerStateObjClass) StateObj;
if(state.TimerCanceled)
{
state.SqlUpdateFromTwitterTimerReference.Dispose();
}
//some work
}
TimerStateObjClass의 * 새 * 개체를 만듭니다. 그럴 수는 없지만 클래스에 비공개 필드를 추가하십시오. –
to TimerStateObjClass? TimerStateObjClass 유형의 – Damo
입니다. –