“新的System.Timers.ElapsedEventHandler(DoStuff)"通话不起作用 [英] "new System.Timers.ElapsedEventHandler(DoStuff)" call did not work

查看:41
本文介绍了“新的System.Timers.ElapsedEventHandler(DoStuff)"通话不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#中创建监视文件夹应用程序,该应用程序将在新文件到达时执行操作.由于监视的文件夹位于GPFS共享上,因此我无法使用FileSystemWatcher(在NTFS中对我来说效果很好).因此,我已将该应用基于其他同事

I'am trying to create watch folder aplicaction in C# that will do an action when new file will arrive. Since the watched folder is on GPFS share I'am unable to use FileSystemWatcher (which works fine for me in NTFS). So I've based the app on other collegue solution. The app shows nicely "Timer starts" message but when it comes to

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

它没有调用DoStuff方法-永远不会显示正在启动新文件proc"消息.我做错了什么?这是完整的代码:

it did not calls the DoStuff method - "Starting new files proc" message never show up. What I've done wrong? Here is complete code:

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)
        {

        }
    }
}

推荐答案

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

There are few issues in your code.

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

First of all you are not setting the time at which timer should elapse, which means it will read the default value which is

100毫秒

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

Secondly you are not starting your timer. You need to add this line to your code in this method startActivityMonitoring else statement.

timer.Interval = yourdesiredinterval;

timer.Interval = yourdesiredinterval;

timer.Start();

timer.Start();

第三,在进行停止和启动时(根据代码的外观),不应在每次调用startActivityMonitoring方法时都创建一个新计时器.而是应该这样做

Thirdly, as you are doing stop and start (by looks of your code) you should not create a new timer on each call of your startActivityMonitoring method. Rather you should do this

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

这篇关于“新的System.Timers.ElapsedEventHandler(DoStuff)"通话不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆