【问题标题】:Raise event from one class to another将事件从一个班级提升到另一个班级
【发布时间】:2017-05-15 09:15:29
【问题描述】:

所以我想开始检查从我的class 过去的时间,当这个time 过去时(1 分钟)我想引发事件:

public class TimeOut
{
    private readonly int TIME_OUT = 60;
    private System.Timers.Timer _timer;
    private Stopwatch _stopwatch;
    public event TimeOutHandler TimePassed;

    public void Initiate()
    {
        _timer = new System.Timers.Timer();
        _timer.Interval = 1000;
        _timer.Elapsed += _timer_Elapsed;
        _stopwatch = new Stopwatch();
    }

    public void Start()
    {
        _stopwatch.Start();
        _timer.Start();
    }

    public void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (_stopwatch.ElapsedMilliseconds / 1000 >= TIME_OUT)
        {
            if (TimePassed!=null)
            {
                TimePassed(sender, null);
                Stop();
            }
        }
    }

    public void Stop()
    {
        _timer.Stop();
        _stopwatch.Stop();
    }
}

用法

TimeOut timeOut = new TimeOut();
timeOut.TimePassed += timeOut_TimePassed;
timeOut.Initiate();
timeOut.Start();

private void timeOut_TimePassed(object sener, System.EventArgs e)
{

}

所以我不知道为什么,但我的 timer elapsed 函数似乎永远不会启动。 我尝试使用_timer.Enabled = true; 而不是_timer.Start();,但这仍然有帮助。

【问题讨论】:

  • TimePassed(sender, null); 最好传递EventArgs.Empty 而不是nullTimePassed(sender, EventArgs.Empty);

标签: c# timer console stopwatch


【解决方案1】:

等待 1 分钟后,它为我按下了功能。

但是,您的代码不是通过复制和粘贴来编译的。将事件更改为委托,我必须将您的私有方法设为静态。

也不确定您问题的最后一点,但至少在我的情况下,控制台会立即关闭。所以我在最后添加了 Console.Read() 以等待计时器过去。 var x 只是一个断点

希望对你有帮助

using System;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System.Timers;


namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            TimeOut timeOut = new TimeOut();
            timeOut.TimePassed += timeOut_TimePassed;
            timeOut.Initiate();
            timeOut.Start();

            Console.Read();
        }

        // Changed this
        private static void timeOut_TimePassed(object sener, System.EventArgs e)
        {
            var x = 1;
        }
    }

    public class TimeOut
    {
        private readonly int TIME_OUT = 60;
        private System.Timers.Timer _timer;
        private Stopwatch _stopwatch;
        // Changed this
        public delegate void TimeOutHandler(object sener, EventArgs e);
        public event TimeOutHandler TimePassed;

        public void Initiate()
        {
            _timer = new System.Timers.Timer();
            _timer.Interval = 1000;
            _timer.Elapsed += _timer_Elapsed;
            _stopwatch = new Stopwatch();
        }

        public void Start()
        {
            _stopwatch.Start();
            _timer.Start();
        }

        public void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (_stopwatch.ElapsedMilliseconds / 1000 >= TIME_OUT)
            {
                if (TimePassed != null)
                {
                    TimePassed(sender, null);
                    Stop();
                }
            }
        }

        public void Stop()
        {
            _timer.Stop();
            _stopwatch.Stop();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多