2013-10-20 4 views
1

Windows Form 프로그램 (사용자 로그인이있는 E-POS)을 개발했지만 요구 사항으로 30 분 후에 시스템에서 사용자를 로그 아웃하려고합니다. 나는 다음과 같은 코드를 발견하고 주위 연주,하지만 난 오류 얻을 : 코드에 나타나는 _TimerTick의 첫 번째 인스턴스에 의해일정 시간이 지난 후 사용자 로그 아웃

a field initializer cannot reference the non-static field, method or property...

합니다.

private System.Threading.Timer timer = new System.Threading.Timer(
    _TimerTick, 
    null, 
    1000 * 30 * 60, 
    Timeout.Infinite); 

private void _OnUserActivity(object sender, EventArgs e) 
{ 
    if (timer != null) 
    { 
     timer.Change(1000 * 30 * 60, Timeout.Infinite); 
    } 
} 

private void _TimerTick(object state) 
{ 
    var myLogin = new LoginForm(this); 
    myLogin.userCode = null; 
    MainControlsPanel.Hide(); 

    // the user has been inactive for 30 minutes; log him out 
} 
+1

문제는 정확하게 말합니다. 타이머 초기화를 생성자로 이동하십시오. – BartoszKP

답변

0

내 의견은 당신의 클래스 생성자에 타이머를 할당하는 것입니다 :

대신

private System.Threading.Timer timer = new System.Threading.Timer(
_TimerTick, 
null, 
1000 * 30 * 60, 
Timeout.Infinite); 

의 내가 그런 걸 사용합니다 :

class YourClass 
{ 
private System.Threading.Timer timer; 
public YourClass() 
{ 
    timer = new System.Threading.Timer(
    _TimerTick, 
    null, 
    1000 * 30 * 60, 
    Timeout.Infinite); 
} 
//... 
} 

을 또는이 another 보행시 방법

관련 문제