1

msdn の例です。

public class Timer1
{
    public static void Main()
    {
        System.Timers.Timer aTimer = new System.Timers.Timer();
        aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
        // Set the Interval to 5 seconds.
        aTimer.Interval=5000;
        aTimer.Enabled=true;

        Console.WriteLine("Press \'q\' to quit the sample.");
        while(Console.Read()!='q');
    }

    // Specify what you want to happen when the Elapsed event is raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Hello World!");
    }
}

私の質問はこの行に向けられています:

private static void OnTimedEvent(object source, ElapsedEventArgs e)

'source' 変数と 'e' 変数には何が隠されていますか? それらが関数パラメーターであることは知っていますが、イベントからそれらに送信されるものは何ですか?

4

3 に答える 3

2

sourceは、イベントを呼び出したオブジェクトである必要があります。この場合はTimer

eイベントに関するより多くのメタ情報を含める必要があります 。

たとえば、on-clickイベントがある...EventArgs場合、クリックが発生した座標がわかる場合があります。

Visual Studio を使用している場合は、次のようe.に入力すると、そこにあるすべての情報がインテリジェンスによって表示されます。

于 2013-02-27T16:27:15.843 に答える
2

Source is the source of the event - in this case the timer; while e contains information relating to the event. In this case ElapsedEventArgs:

http://msdn.microsoft.com/en-us/library/system.timers.elapsedeventargs_members(v=vs.80).aspx

Another example would be the keyDown event for a textbox in winforms - the e parameter gives you the KeyCode and lets you determine if the user was holidng Alt/Control, etc.

 txt_KeyDown(object sender, KeyEventArgs e)
 {
   if (e.KeyCode == Keys.Enter)
   ...
于 2013-02-27T16:28:09.240 に答える
1

sourceタイマーそのものになります。eイベントがトリガーされた時刻を通知します:ElapsedEventArgs

ほとんどの場合、これらの議論のいずれにも関心はありません。タイマーがトリガーされたときに何かをすることだけを気にするので、メソッドに渡された引数を喜んで無視できます。

于 2013-02-27T16:29:08.647 に答える