定时器事件未触发按钮点击

3
当我点击“开始”按钮时,我正在尝试每3秒从源目录传输一个图像文件到目标目录。当我点击“停止”按钮时,文件传输将停止。以下是我的进展:
1. 开始——文件传输;按下停止按钮 // 它也有效。 2. 开始——文件传输;按下停止按钮 // 它也有效。 3. 开始——文件未传输,事件未触发 // 它不起作用!
我为解决问题所做的事情:
- 我在Tick事件中设置了断点,但Tick事件未触发。 - 我检查了SourceFiles.Count是否大于TransferCount(SourceFiles.Count > TransferCount)。 - 我注意到,在文件传输期间,如果我单击停止按钮,则该特定文件保持未传输状态。
请问如何解决这个问题?谢谢。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Threading;

namespace GridTest
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    public partial class Window3 : Window
    {
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        private static List<string> SourceFiles = new List<string>();
        private static readonly string SourceDir = @"C:\TestFiles\Images\";
        private static readonly string DestinationDir = @"C:\Files\Images\3_5x5\";
        private static int TransferCount = 0;
        public Window3()
        {
            InitializeComponent();
            this.Loaded += Window3_Loaded;
        }
        void Window3_Loaded(object sender, RoutedEventArgs e)
        {
            dispatcherTimer.Interval = new TimeSpan(0, 0, 3);
            dispatcherTimer.Tick += dt_Tick;
        }
        void dt_Tick(object sender, EventArgs e)
        {
            if (TransferCount < SourceFiles.Count)
            {
                var fileName = System.IO.Path.GetFileName(SourceFiles[TransferCount]);
                var destFile = System.IO.Path.Combine(DestinationDir, fileName);
                System.IO.File.Copy(SourceFiles[TransferCount], destFile,true);
                System.IO.File.Delete(SourceFiles[TransferCount]);
                Console.WriteLine(string.Format("Total Files: {0} Number of files transferred: {1}", SourceFiles.Count, TransferCount + 1));
                TransferCount += 1;
            }
            else
            {
                Console.WriteLine(string.Format("Total number of files transferred: {0}. Transfer Completed", TransferCount + 1));
                (sender as DispatcherTimer).Stop();
                (sender as DispatcherTimer).Tick -= dt_Tick;
            }
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            if (!System.IO.Directory.Exists(DestinationDir))
            {
                System.IO.Directory.CreateDirectory(DestinationDir);
            }

            if (System.IO.Directory.Exists(SourceDir))
            {
                SourceFiles = Directory.GetFiles(SourceDir).ToList();
            }
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            dispatcherTimer.Stop();
        }
    }
}

在调用 Stop_Click 后,你如何重新启动计时器? - Steve
@Steve:我需要重新启动计时器吗? - linguini
你的目标.NET运行时版本是什么? - noseratio - open to work
@linguini,你在使用VS2012+吗?你的代码场景非常适合使用async/await进行重构。 - noseratio - open to work
@Noseratio:是的,我正在使用VS2012。 - linguini
3个回答

2
如果您调用“Stop”方法,则应在将“IsEnabled”属性设置为“true”之前防止定时器事件触发。因此,我建议进行一些重构。
    void Window3_Loaded(object sender, RoutedEventArgs e)
    {
        LoadSourceFiles();
        StartTimer();
    }
    void StartTimer()
    {
        dispatcherTimer.Interval = new TimeSpan(0, 0, 3);
        dispatcherTimer.Tick += dt_Tick;
        dispatcherTimer.IsEnabled = true;
    }
    void LoadSourceFiles()
    {
        if (!System.IO.Directory.Exists(DestinationDir))
        {
            System.IO.Directory.CreateDirectory(DestinationDir);
        }

        if (System.IO.Directory.Exists(SourceDir))
        {
            SourceFiles = Directory.GetFiles(SourceDir).ToList();
        }
    }

每次按下“开始”按钮时,请调用此方法。

    private void Start_Click(object sender, RoutedEventArgs e)
    {
        StartTimer();
    }

    private void Stop_Click(object sender, RoutedEventArgs e)
    {
        dispatcherTimer.IsEnabled = false;
    }

我建议在Tick事件中发现所有文件已传输完成时,将跟踪传输文件的全局变量重置为零(或禁用“开始”按钮)。

    ....
    else
    {
          Console.WriteLine(string.Format("Total number of files transferred: {0}. Transfer Completed", TransferCount + 1));
          (sender as DispatcherTimer).Stop();
          (sender as DispatcherTimer).Tick -= dt_Tick;
          TransferCount = 0;
    }

否则,如果在这种情况下重新启动计时器,它将立即停止。

有趣的是,第一次点击传输1个文件,第二次点击传输2个文件;第三次点击,传输3个文件...第四次点击传输4个文件。 - linguini
你的意思是说,如果你点击“开始”,只会复制一个文件,然后事件就不会再次触发了吗? - Steve
这是因为保存要传输的文件索引的变量被打印出来,就像它是在单个tick事件中传输的文件计数一样。消息应该类似于“已传输”+ TransferCount.ToString() +“个文件,共”+ SourceFiles.Count.ToString() +“个要传输”。 - Steve
太好了!我没有考虑到这个变量。我猜这就是它应该工作的方式,我们不能更改更多了吧?如果我错了,请纠正我。 - linguini
让我们在聊天室继续这个讨论:点击此处 - linguini
显示剩余4条评论

2

虽然不是答案,但这里有一个重构版本,它不使用定时器。相反,它使用TaskEx.Delayasync/await,未经测试(针对.NET 4.0,使用vs2012+和Microsoft.Bcl.Async):

using System;
using System.Collections.Generic;
using System.Windows;
using System.Threading;
using System.Threading.Tasks;

namespace GridTest
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    public partial class Window3 : Window
    {
        private static List<string> SourceFiles = new List<string>();
        private static readonly string SourceDir = @"C:\TestFiles\Images\";
        private static readonly string DestinationDir = @"C:\Files\Images\3_5x5\";
        public Window3()
        {
            InitializeComponent();
            this.Loaded += Window3_Loaded;
        }
        void Window3_Loaded(object sender, RoutedEventArgs e)
        {
        }

        async Task DoWorkAsync(CancellationToken token)
        {
            int transferCount;
            for (transferCount = 0; transferCount < SourceFiles.Count; transferCount++)
            {
                token.ThrowIfCancellationRequested();
                var fileName = System.IO.Path.GetFileName(SourceFiles[transferCount]);
                var destFile = System.IO.Path.Combine(DestinationDir, fileName);
                System.IO.File.Copy(SourceFiles[transferCount], destFile, true);
                System.IO.File.Delete(SourceFiles[transferCount]);
                Console.WriteLine(string.Format("Total Files: {0} Number of files transferred: {1}", SourceFiles.Count, transferCount + 1));
                transferCount += 1;

                await TaskEx.Delay(3000, token); // 3s delay
            }
            Console.WriteLine(string.Format("Total number of files transferred: {0}. Transfer Completed", transferCount + 1));
        }

        CancellationTokenSource _cts;
        Task _task;

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            if (_cts != null)
                _cts.Cancel();
            _cts = new CancellationTokenSource();
            _task = TaskEx.Run(() => DoWorkAsync(_cts.Token), _cts.Token);
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            if (_cts != null)
                _cts.Cancel();
        }
    }
}

1
谢谢您提供的示例。我会尝试这种方法,这对我理解这个概念很有帮助。 - linguini

1

你从未在DispatchTimer上调用Start

将此添加到Window3_Loaded方法中:

dispatcherTimer.Start();

我已经添加了 dispatcherTimer.Start(); 但仍然存在同样的问题。第三次点击时,Tick事件没有触发。 - linguini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接