【问题标题】:Process is started twice in the OnChanged event进程在 OnChanged 事件中启动两次
【发布时间】:2015-08-18 12:17:10
【问题描述】:

我创建了一个程序来使用 filewatcher 监视记事本文件.. 每当文件中的文本发生更改时.. 我制作了一个 .exe 程序来运行... .exe 程序也可以正常运行.. 但它运行了两次...我搜索了 Stackoverflow 和其他网站.. 但我不明白我哪里出错了.. Frnds Plz 帮助我清除我的错误.. 我知道这个问题已经被问过很多次了.. 但是因为我是新手c#我听不懂..请帮我看看..

    public Form1()
    {
        InitializeComponent();
    }

    private void filewatcher()
    {
        path = txtBoxPath.Text;
        FileSystemWatcher fileSystemWatcher1 = new FileSystemWatcher();
        fileSystemWatcher1.Path = path;
        fileSystemWatcher1.Filter = "*.txt";
        fileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite;
        fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
        fileSystemWatcher1.EnableRaisingEvents = true;
    }

    private void tbtextcopy()
    {
        Clipboard.SetText(File.ReadAllText(path));
        this.textBox1.Text = Clipboard.GetText().ToString();
    }
    private void OnChanged(object sender, FileSystemEventArgs e)
    {

        try
        {
            ProcessStartInfo PSI = new ProcessStartInfo(@"C:\Program Files\Ranjhasoft\Icon Changer\Icon changer.exe");
            Process P = Process.Start(PSI);
            P.WaitForExit();
            P.Close();
        }

        finally
        {
            fileSystemWatcher1.EnableRaisingEvents = false;
        }

    }

    private void BrowserBtn_Click(object sender, EventArgs e)
    {

        FolderBrowserDialog dlg = new FolderBrowserDialog();
        dlg.SelectedPath = this.txtBoxPath.Text;
        DialogResult result = dlg.ShowDialog();

        if (result == DialogResult.OK)
        {
            if (!string.IsNullOrEmpty(dlg.SelectedPath))
            {
                this.txtBoxPath.Text = dlg.SelectedPath;
            }
        }

        if (string.IsNullOrEmpty(this.txtBoxPath.Text))
        {
            this.txtBoxPath.Text = appDataFolder;
        }
        if (!Directory.Exists(path))
        {
            MessageBox.Show("Specified folder does not exist");
        }
    }

    private void txtBoxPath_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.txtBoxPath.Text))
        {
            this.filewatcher();
        }
    }

}

}

【问题讨论】:

  • 所以@sstan 我应该使用这个fileSystemWatcher1.EnableRaisingEvents = true;在尝试命令而不是 filewatcher()
  • 我也看过那篇文章。我想知道“我”哪里出错了...
  • 我可以知道你得到了-2吗?

标签: c# forms file filesystemwatcher


【解决方案1】:

你有代码

fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);

您可以重复地将相同的事件处理程序添加到事件中。是的,它可以执行 20 次。

您只需将其设置为运行一次代码,因此只需添加一次。如果您停止触发事件,则事件处理程序不会擦除。所以设置事件处理程序一次,而不是每次都在文件观察器方法中设置,这将重复添加相同的处理程序并产生多个响应。

所以在这里更改您的代码:

public Form1()
{
    InitializeComponent();
    fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
}

并将其从 filewatcher 方法中删除。现在它会在 raiseevents 设置为 true 时运行一次,而在它不是时则根本不运行。

【讨论】:

  • 谢谢@BugFinder 我应该如何让它运行一次..?请解释清楚。如果我很笨,请提前抱歉..
  • 虽然我可以编辑您的代码,但这无济于事,因为阅读您的问题的其他人会想知道您在说什么……看到问题和答案会更有意义。
  • 谢谢你.. @BugFinder 它没有运行两次.. 但事件会再次运行.. 每当记事本中的文本发生变化时?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多