다음을 통해 공유


Timer 클래스

정의

사용자 정의 간격으로 이벤트를 발생시키는 타이머를 구현합니다. 이 타이머는 Windows Forms 애플리케이션에서 사용하도록 최적화되어 있으며 창에서 사용해야 합니다.

public ref class Timer : System::ComponentModel::Component
public class Timer : System.ComponentModel.Component
type Timer = class
    inherit Component
Public Class Timer
Inherits Component
상속

예제

다음 예제에서는 5초마다 알람을 설정하는 간단한 간격 타이머를 구현합니다. 알람이 발생하면 MessageBox 알람이 시작된 횟수를 표시하고 타이머가 계속 실행되어야 하는지 여부를 사용자에게 알립니다.

public ref class Class1
{
private:
   static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer;
   static int alarmCounter = 1;
   static bool exitFlag = false;

   // This is the method to run when the timer is raised.
   static void TimerEventProcessor( Object^ /*myObject*/, EventArgs^ /*myEventArgs*/ )
   {
      myTimer->Stop();
      
      // Displays a message box asking whether to continue running the timer.
      if ( MessageBox::Show( "Continue running?", String::Format( "Count is: {0}", alarmCounter ), MessageBoxButtons::YesNo ) == DialogResult::Yes )
      {
         
         // Restarts the timer and increments the counter.
         alarmCounter += 1;
         myTimer->Enabled = true;
      }
      else
      {
         
         // Stops the timer.
         exitFlag = true;
      }
   }


public:
   static void Main()
   {
      
      /* Adds the event and the event handler for the method that will 
                process the timer event to the timer. */
      myTimer->Tick += gcnew EventHandler( TimerEventProcessor );
      
      // Sets the timer interval to 5 seconds.
      myTimer->Interval = 5000;
      myTimer->Start();
      
      // Runs the timer, and raises the event.
      while ( !exitFlag )
      {
         
         // Processes all the events in the queue.
         Application::DoEvents();
      }
   }

};

int main()
{
   Class1::Main();
}
public class Class1 {
    static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    static int alarmCounter = 1;
    static bool exitFlag = false;
 
    // This is the method to run when the timer is raised.
    private static void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs) {
       myTimer.Stop();
 
       // Displays a message box asking whether to continue running the timer.
       if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter, 
          MessageBoxButtons.YesNo) == DialogResult.Yes) {
          // Restarts the timer and increments the counter.
          alarmCounter +=1;
          myTimer.Enabled = true;
       }
       else {
          // Stops the timer.
          exitFlag = true;
       }
    }
 
    public static int Main() {
       /* Adds the event and the event handler for the method that will 
          process the timer event to the timer. */
       myTimer.Tick += new EventHandler(TimerEventProcessor);
 
       // Sets the timer interval to 5 seconds.
       myTimer.Interval = 5000;
       myTimer.Start();
 
       // Runs the timer, and raises the event.
       while(!exitFlag) {
          // Processes all the events in the queue.
          Application.DoEvents();
       }
    return 0;
    }
 }
Public Class Class1
    Private Shared WithEvents myTimer As New System.Windows.Forms.Timer()
    Private Shared alarmCounter As Integer = 1
    Private Shared exitFlag As Boolean = False    
    
    ' This is the method to run when the timer is raised.
    Private Shared Sub TimerEventProcessor(myObject As Object, _
                                           ByVal myEventArgs As EventArgs) _
                                       Handles myTimer.Tick
        myTimer.Stop()
        
        ' Displays a message box asking whether to continue running the timer.
        If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, _
                            MessageBoxButtons.YesNo) = DialogResult.Yes Then
            ' Restarts the timer and increments the counter.
            alarmCounter += 1
            myTimer.Enabled = True
        Else
            ' Stops the timer.
            exitFlag = True
        End If
    End Sub
    
    Public Shared Sub Main()
        ' Adds the event and the event handler for the method that will
        ' process the timer event to the timer.
        
        ' Sets the timer interval to 5 seconds.
        myTimer.Interval = 5000
        myTimer.Start()
        
        ' Runs the timer, and raises the event.
        While exitFlag = False
            ' Processes all the events in the queue.
            Application.DoEvents()
        End While

    End Sub    

End Class

설명

A Timer 는 사용자 정의 간격으로 이벤트를 발생 시에 사용 됩니다. 이 Windows 타이머는 UI 스레드가 처리를 수행하는 데 사용되는 단일 스레드 환경을 위해 설계되었습니다. 사용자 코드에는 UI 메시지 펌프를 사용할 수 있고 항상 동일한 스레드에서 작동하거나 호출을 다른 스레드로 마샬링해야 합니다.

이 타이머를 Tick 사용하는 경우 이벤트를 사용하여 폴링 작업을 수행하거나 지정된 기간 동안 시작 화면을 표시합니다. 속성이 Enabled 설정 true 되고 Interval 속성이 0 Tick 보다 클 때마다 속성 설정에 Interval 따라 이벤트가 간격으로 발생합니다.

이 클래스는 간격을 설정하고 타이머를 시작하고 중지하는 메서드를 제공합니다.

메모

Windows Forms 타이머 구성 요소는 단일 스레드이며 55밀리초의 정확도로 제한됩니다. 더 높은 정확도의 다중 스레드 타이머가 필요한 경우 네임스페이 Timer 스의 클래스를 System.Timers 사용합니다.

생성자

Name Description
Timer()

Timer 클래스의 새 인스턴스를 초기화합니다.

Timer(IContainer)

지정된 컨테이너와 함께 클래스의 Timer 새 인스턴스를 초기화합니다.

속성

Name Description
CanRaiseEvents

구성 요소가 이벤트를 발생시키는지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 Component)
Container

IContainer 포함하는 값을 가져옵니다 Component.

(다음에서 상속됨 Component)
DesignMode

현재 디자인 모드인지 여부를 Component 나타내는 값을 가져옵니다.

(다음에서 상속됨 Component)
Enabled

타이머가 실행 중인지 여부를 가져오거나 설정합니다.

Events

Component에 연결된 이벤트 처리기 목록을 가져옵니다.

(다음에서 상속됨 Component)
Interval

이벤트의 마지막 발생 Tick 을 기준으로 이벤트가 발생하기 전의 Tick 시간(밀리초)을 가져오거나 설정합니다.

Site

ISite값을 Component 가져오거나 설정합니다.

(다음에서 상속됨 Component)
Tag

일부 유형의 사용자 상태를 나타내는 임의의 문자열을 가져오거나 설정합니다.

메서드

Name Description
CreateObjRef(Type)

원격 개체와 통신하는 데 사용되는 프록시를 생성하는 데 필요한 모든 관련 정보를 포함하는 개체를 만듭니다.

(다음에서 상속됨 MarshalByRefObject)
Dispose()

에서 사용하는 모든 리소스를 Component해제합니다.

(다음에서 상속됨 Component)
Dispose(Boolean)

타이머에서 사용하는 메모리 이외의 리소스를 삭제합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 여부를 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 사용됩니다.

(다음에서 상속됨 Object)
GetLifetimeService()
사용되지 않음.

이 인스턴스의 수명 정책을 제어하는 현재 수명 서비스 개체를 검색합니다.

(다음에서 상속됨 MarshalByRefObject)
GetService(Type)

또는 해당 서비스에서 제공하는 서비스를 나타내는 개체를 Component 반환합니다 Container.

(다음에서 상속됨 Component)
GetType()

현재 인스턴스의 Type 가져옵니다.

(다음에서 상속됨 Object)
InitializeLifetimeService()
사용되지 않음.

이 인스턴스의 수명 정책을 제어하는 수명 서비스 개체를 가져옵니다.

(다음에서 상속됨 MarshalByRefObject)
MemberwiseClone()

현재 Object단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
MemberwiseClone(Boolean)

현재 MarshalByRefObject 개체의 단순 복사본을 만듭니다.

(다음에서 상속됨 MarshalByRefObject)
OnTick(EventArgs)

Tick 이벤트를 발생시킵니다.

Start()

타이머를 시작합니다.

Stop()

타이머를 중지합니다.

ToString()

를 나타내는 문자열을 Timer반환합니다.

이벤트

Name Description
Disposed

구성 요소가 메서드 호출에 Dispose() 의해 삭제될 때 발생합니다.

(다음에서 상속됨 Component)
Tick

지정된 타이머 간격이 경과하고 타이머를 사용할 때 발생합니다.

적용 대상