Timer.Stop 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
타이머를 중지합니다.
public:
void Stop();
public void Stop();
member this.Stop : unit -> unit
Public Sub Stop ()
예제
다음 코드 예제에서는 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
설명
속성을 .로 설정하여 타이머를 중지할 Enabledfalse수도 있습니다.
Timer 동일한 애플리케이션 세션 내에서 개체를 여러 번 사용하도록 설정하고 사용하지 않도록 설정할 수 있습니다.
호출 Start 을 Timer 사용하지 않도록 설정한 후 호출 Stop 하면 중단된 간격이 Timer 다시 시작됩니다. Timer 5000밀리초 간격으로 설정되고 약 3,000밀리초 단위로 호출 Stop 하는 경우 호출 Start 하면 이벤트를 발생 Tick 하기 전에 5,000밀리초 동안 대기하게 됩니다Timer.
메모
모든 구성 요소가 기본 애플리케이션 스레드에서 작동하기 때문에 Timer Windows Forms 애플리케이션 내에서 중지 Timer 를 호출하면 애플리케이션의 다른 Timer 구성 요소의 메시지가 즉시 처리될 수 있습니다. 구성 Timer 요소가 700밀리초로 설정되고 한 구성 요소가 500밀리초로 설정된 경우 첫 번째 Timer구성 요소를 호출 Stop 하면 애플리케이션에서 두 번째 구성 요소에 대한 이벤트 콜백을 먼저 받을 수 있습니다. 문제가 있는 경우 네임스페이스에서 클래스를 TimerSystem.Threading 대신 사용하는 것이 좋습니다.