c# - "new System.Timers.ElapsedEventHandler(DoStuff)"调用无效

标签 c# .net visual-studio

我正在尝试在 C# 中创建监视文件夹应用程序,它将在新文件到达时执行操作。由于监视的文件夹位于 GPFS 共享上,因此我无法使用 FileSystemWatcher(它在 NTFS 中对我来说工作正常)。所以我将应用程序基于其他同事 solution . 该应用程序很好地显示了“定时器启动”消息,但是当它涉及到

timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff);

它没有调用 DoStuff 方法 - “Starting new files proc”消息从未出现。我做错了什么?完整代码如下:

namespace MonitorFolderActivity
{
    public partial class frmMain : Form
    {
        List<string> fileList = new List<string>();
        System.Timers.Timer timer;
        DateTime LastChecked;

        public frmMain()
        {
            InitializeComponent();
        }
        private void abortAcitivityMonitoring()
        {
            btnStart_Stop.Text = "Start";
            txtActivity.Focus();
        }

        private void startActivityMonitoring(string sPath)
        {
            if (sPath.Length < 3)
            {
                MessageBox.Show("You have to enter a folder to monitor.",
                    "Hey..!", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                abortAcitivityMonitoring();
            }
            else
            {
                TS_AddLogText(string.Format("Timer starts\r\n"));
                timer = new System.Timers.Timer();
                timer.AutoReset = false;

                timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff);
            }
        }

        private void stopActivityMonitoring()
        {
            TS_AddLogText(string.Format("Timer stops\r\n"));
            this.timer.Stop();
        }

        private void DoStuff(object sender, System.Timers.ElapsedEventArgs e)
        {
            TS_AddLogText(string.Format("Starting new files proc\r\n"));
            LastChecked = DateTime.Now;

            string[] files = System.IO.Directory.GetFiles("D:\\MEDIAIN\\", "*", System.IO.SearchOption.AllDirectories);

            foreach (string file in files)
            {
                if (!fileList.Contains(file))
                {
                    fileList.Add(file);
                    TS_AddLogText(string.Format(file));
                }
            }


            TimeSpan ts = DateTime.Now.Subtract(LastChecked);
            TimeSpan MaxWaitTime = TimeSpan.FromMinutes(1);

            if (MaxWaitTime.Subtract(ts).CompareTo(TimeSpan.Zero) > -1)
                timer.Interval = MaxWaitTime.Subtract(ts).TotalMilliseconds;
            else
                timer.Interval = 1;

            timer.Start();
        }

        private delegate void AddLogText(string text);
        private void TS_AddLogText(string text)
        {
            if (this.InvokeRequired)
            {
                AddLogText del = new AddLogText(TS_AddLogText);
                Invoke(del, text);
            }
            else
            {
                txtActivity.Text += text;
            }
        }

        private void btnStart_Stop_Click(object sender, EventArgs e)
        {
            if (btnStart_Stop.Text.Equals("Start"))
            {
                btnStart_Stop.Text = "Stop";
                startActivityMonitoring(txtFolderPath.Text);
            }
            else
            {
                btnStart_Stop.Text = "Start";
                stopActivityMonitoring();
            }
        }

        private void lblActivity_Click(object sender, EventArgs e)
        {

        }

        private void lblToMonitor_Click(object sender, EventArgs e)
        {

        }
    }
}

最佳答案

您的代码中几乎没有问题。

首先您没有设置计时器应该结束的时间,这意味着它将读取默认值

100 ms

其次您没有启动计时器。您需要将此行添加到此方法 startActivityMonitoring else 语句中的代码中。

timer.Interval = yourdesiredinterval;

timer.Start();

第三,当您停止和启动(通过查看您的代码)时,您不应在每次调用 startActivityMonitoring 方法时创建一个新计时器。相反,你应该这样做

If(timer == null)
{
    timer = new System.Timers.Timer();
    timer.AutoReset = false;
    timer.Interval = yourinterval;
    timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff); 
}
timer.Start();

关于c# - "new System.Timers.ElapsedEventHandler(DoStuff)"调用无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17889280/

相关文章:

c# - [标志] 枚举 : long vs [Flags] enum : ulong 之间的意外行为

javascript - 如何使用.net库类运行javascript函数?

c# - Visual Studio 连接字符串- 相对文件路径?

visual-studio - 在哪里可以获得独立的nmake.exe?

visual-studio - Visual Studio CPU Profiler/无数据

c++ - 对同一类的多个不同对象使用 1 个唯一的 MFC 对话框

c# - Windows Phone 通用应用程序在导航时抛出 AccessViolation

c# - 无法在 XNA 中将 Awesomium 渲染到屏幕上

c# - 以编程方式从 PC 识别连接的设备是否为 Android

c# - 将 .NET Standard 库添加到 4.7.1 lib 会增加大量引用,有些已损坏