.NET/指定した時間間隔でイベントを発行する・Timer
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
* タイマーイベントを発行する・Timer [#g22a0415]
「指定した時間間隔で指定した処理をしたい!」~
このような場合はタイマーを使うと簡単に実現できます。~
以下に、C#とVisual Basic(VB)のサンプルコードと実際に実行...
#htmlinsert(windev-top.html)
#contents
* 参考サイト [#z1067e72]
-[[Microsoft|.NET Timer クラス>https://docs.microsoft.com...
-[[Microsoft|.NET ElapsedEventArgs クラス>https://docs.mi...
* 使用環境 [#f757ec0e]
- Windows 10 ver.1909
- Visual Studio 2019
- .NET Core
- コンソールアプリケーション
* サンプルコードの説明 [#o19cffca]
以下に紹介するC#,Visual Basicのサンプルコードの説明をしま...
-Main~
Mainで自分自身(Program)をNewし、Timerの設定を行い、キー入...
-runTimer~
runTimerでTimerクラスをNewし各設定を行っています。~
Timer.Elapsedでは、時間になったら実行するメソッド(timerEv...
Timer.Enabledでタイマーが実行されます。
-timerEventMethod~
経過時間を表示させています。
* Timerのサンプルコード [#xe4fd620]
以下にC#とVisual BasicでTimerを使ったサンプルコードを紹介...
C#ではClassでVisual BasicではModuleで作成しました。
** C# [#q47265de]
using System;
class Program
{
static void Main(string[] args)
{
Program me = new Program();
me.runTimer(2000);
Console.WriteLine("Press Enter to end this proce...
Console.ReadLine();
}
public void runTimer(int msec)
{
System.Timers.Timer tm = new System.Timers.Timer...
tm.Elapsed += timerEventMethod;
tm.AutoReset = true;
tm.Enabled = true;
}
public void timerEventMethod(Object source, System.T...
{
Console.WriteLine("ElapsedEventArgs.SignalTime: ...
}
}
** Visual Basic [#ta94962f]
Imports System
Module Program
Sub Main(args As String())
runTimer(2000)
Console.WriteLine("Press Enter to end this proce...
Console.ReadLine()
End Sub
Sub runTimer(msec As Integer)
Dim tm As System.Timers.Timer = New System.Timer...
AddHandler tm.Elapsed, AddressOf timerEventMethod
tm.AutoReset = True
tm.Enabled = True
End Sub
Sub timerEventMethod(source As Object, e As System.T...
Console.WriteLine("ElapsedEventArgs.SignalTime: ...
End Sub
End Module
** 実行結果 [#d238859e]
上記サンプルコードを実行した時のキャプチャになります。
#ref(01.png)
以上、指定した時間毎にイベントを発生させるタイマークラス...
#htmlinsert(windev-btm.html)
終了行:
#navi(../)
* タイマーイベントを発行する・Timer [#g22a0415]
「指定した時間間隔で指定した処理をしたい!」~
このような場合はタイマーを使うと簡単に実現できます。~
以下に、C#とVisual Basic(VB)のサンプルコードと実際に実行...
#htmlinsert(windev-top.html)
#contents
* 参考サイト [#z1067e72]
-[[Microsoft|.NET Timer クラス>https://docs.microsoft.com...
-[[Microsoft|.NET ElapsedEventArgs クラス>https://docs.mi...
* 使用環境 [#f757ec0e]
- Windows 10 ver.1909
- Visual Studio 2019
- .NET Core
- コンソールアプリケーション
* サンプルコードの説明 [#o19cffca]
以下に紹介するC#,Visual Basicのサンプルコードの説明をしま...
-Main~
Mainで自分自身(Program)をNewし、Timerの設定を行い、キー入...
-runTimer~
runTimerでTimerクラスをNewし各設定を行っています。~
Timer.Elapsedでは、時間になったら実行するメソッド(timerEv...
Timer.Enabledでタイマーが実行されます。
-timerEventMethod~
経過時間を表示させています。
* Timerのサンプルコード [#xe4fd620]
以下にC#とVisual BasicでTimerを使ったサンプルコードを紹介...
C#ではClassでVisual BasicではModuleで作成しました。
** C# [#q47265de]
using System;
class Program
{
static void Main(string[] args)
{
Program me = new Program();
me.runTimer(2000);
Console.WriteLine("Press Enter to end this proce...
Console.ReadLine();
}
public void runTimer(int msec)
{
System.Timers.Timer tm = new System.Timers.Timer...
tm.Elapsed += timerEventMethod;
tm.AutoReset = true;
tm.Enabled = true;
}
public void timerEventMethod(Object source, System.T...
{
Console.WriteLine("ElapsedEventArgs.SignalTime: ...
}
}
** Visual Basic [#ta94962f]
Imports System
Module Program
Sub Main(args As String())
runTimer(2000)
Console.WriteLine("Press Enter to end this proce...
Console.ReadLine()
End Sub
Sub runTimer(msec As Integer)
Dim tm As System.Timers.Timer = New System.Timer...
AddHandler tm.Elapsed, AddressOf timerEventMethod
tm.AutoReset = True
tm.Enabled = True
End Sub
Sub timerEventMethod(source As Object, e As System.T...
Console.WriteLine("ElapsedEventArgs.SignalTime: ...
End Sub
End Module
** 実行結果 [#d238859e]
上記サンプルコードを実行した時のキャプチャになります。
#ref(01.png)
以上、指定した時間毎にイベントを発生させるタイマークラス...
#htmlinsert(windev-btm.html)
ページ名: