c#使用System.Timers时取消任务

时间:2019-02-10 00:30:48

标签: c# cancellationtokensource system.timers.timer

我不确定如何最好地取消正在运行系统计时器的任务。 在下面的代码中,计时器每隔60分钟运行一次,然后运行另一种方法(CheckFileOverflow),该方法用于检查系统日志txt的文件大小。文件

取消计时器最好通过单击按钮或调用取消的其他方法来完成。只要软件在运行,计时器就可以有效地运行,但是当用户最终关闭软件时,我希望能够以负责任的方式取消任务,即不存在正在进行的线程池的风险资源在后台挥之不去。

我花了很多时间来阅读取消令牌,但仍然不明白:(

public void SystemEventLoggerTimer()
    {
        SysEvntLogFileChckTimerRun = true;

        Task.Run(() =>
        {
            System.Timers.Timer timer = new System.Timers.Timer
            { Interval = 1000 * 60 * 60 };
            timer.Elapsed += new ElapsedEventHandler(CheckFileOverflow);
            timer.Start();
        });
    }

4 个答案:

答案 0 :(得分:2)

我建议您使用Microsoft的Reactive Framework(又名Rx)-仅使用NuGet System.Reactive

然后您执行以下操作:

IDisposable subscription =
    Observable
        .Interval(TimeSpan.FromHours(1.0))
        .Subscribe(_ => CheckFileOverflow());

要取消订阅时,只需致电subscription.Dispose()

Rx是抽象计时器,事件,任务,异步操作等的理想选择。

答案 1 :(得分:0)

您可以将方法更改为类似

  public void SystemEventLoggerTimer(CancellationToken cancelToken)
        {
            SysEvntLogFileChckTimerRun = true;

            Task.Run(async () =>
            {
                // Keep this task alive until it is cancelled
                while (!cancelToken.IsCancellationRequested)
                {
                    await Task.Delay(TimeSpan.FromMinutes(60));
                    CheckFileOverflow();
                }
            });
        }

然后您像这样调用SystemEventLoggerTimer

var cancelSource = new CancellationTokenSource();
SystemEventLoggerTimer(cancelSource.Token);

您可以在程序被处置时或仅在主要功能结束时取消该令牌

答案 2 :(得分:0)

为什么不只在调用上下文中(或在您的类/应用程序中全局)访问计时器-无论如何,您还是必须使用CancellationTokenSource来执行此操作!这似乎不适合Task用例。

尝试一下:

    public void SystemEventLoggerTimer(System.Timers.Timer timer)
    {
        SysEvntLogFileChckTimerRun = true;

        timer.Elapsed += new ElapsedEventHandler(CheckFileOverflow);
        timer.Start();
    }

呼叫代码:

    var timer = new System.Timers.Timer() { Interval = 1000 * 60 * 60 };
    SystemEventLoggerTimer(timer);

取消代码(在取消按钮的事件处理程序中,等等):

    timer.Stop();

答案 3 :(得分:0)

我在下面发布了似乎对我有用的令人满意的解决方案。希望我以正确的方式响应线程...(stackOverflow的新手) 我设置了一个快速的Windows窗体进行测试,创建了2qty按钮和1qty文本框。 按钮用于启动和停止计时器(使用取消令牌) 该文本框用于监视计时器,该计时器每2秒更新一次“计时器正在运行”消息。希望这可以帮助其他人在类似情况下...

enter image description here

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private CancellationTokenSource cancelSource;

    // Button is used to START the timer.
    private void TimerStartButton_Click(object sender, EventArgs e)
    {
        cancelSource = new CancellationTokenSource();
        // Run the below method that will initiate timer to start running from 
        // the button click.
        SystemEventLoggerTimer(cancelSource.Token);
    }

    private void SystemEventLoggerTimer(CancellationToken cancelToken)
    {
        Task.Run(async () =>
        {
            // Keep this task alive until it is cancelled
            while (!cancelToken.IsCancellationRequested)
            {
                // Encapsulating the function Task.Delay with 'cancelToken'
                // allows us to stop the Task.Delay during mid cycle.
                // For testing purposes, have reduced the time interval to 2 secs.
                await Task.Delay(TimeSpan.FromSeconds(2), cancelToken);
                // Run the below method every 2 seconds.
                CheckFileOverflow();
            }
        });
    }

    // When the below method runs every 2 secs, the UpdateUI will allow
    // us to modify the textbox form controls from another thread.
    private void CheckFileOverflow()
    {
        UpdateTextbox("Timer Running");
    }

    // UpdateUI will allow us to modify the textbox form controls from another thread.
    private void UpdateTextbox(string s)
    {
        Func<int> del = delegate ()
        {
            textBox1.AppendText(s + Environment.NewLine);
            return 0;
        };
        Invoke(del);
    }

    // Button that is used to STOP the timer running.
    private void TimerStopButton_Click(object sender, EventArgs e)
    {
        // Initiate the cancelleation request to method "SystemEventLoggerTimer"
        cancelSource.Cancel();
    }
}