事件触发了两次

时间:2013-01-10 01:28:56

标签: c# events

我会尽力解释我的情况。

我正在使用C#开发一个软件,允许多个用户同时在一个公共目录下编辑同一个文件,并查看其他用户所做的更改。

所以我使用FileSystemWatcher监视文件中的更改(更新其他人的更改)和文本框的textchanged(以保存对文件的更改,以便其他人的屏幕也会更新)。

如果我输入字符,它会工作(两个事件都被触发一次) 如果我尝试删除任何形式的字符(退格键,删除等)它不起作用它不会删除任何字符,并且光标总是被重置为位置0.我使用box.SelectionStart移动光标,它在我工作时输入字符。

我把一个计数器有点检查,我发现当我试图删除字符时,两个事件都会被触发两次。

我试图搜索,但我的答案很复杂......

提前致谢

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;`enter code here`
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Forms;
    using System.IO;
    using System.Windows.Threading;

    namespace SharedFileEditor
    {
        public partial class EditorView : Window
        {
            private EditorModel model;
            private FileSystemWatcher watcher;
            private string path;
            private int count = 0;
            private int count2 = 0;

            public EditorView()
            {
                InitializeComponent();
                model = new EditorModel();
                this.DataContext = model;
            }

            private void OpenClicked(object sender, RoutedEventArgs e)
            {
                using (OpenFileDialog dialog = new OpenFileDialog())
                {
                    dialog.Filter = "Text files (*.txt)|*.txt";
                    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        watcher = new FileSystemWatcher(System.IO.Path.GetDirectoryName(dialog.FileName), "*.txt");
                        Console.WriteLine(System.IO.Path.GetDirectoryName(dialog.FileName));
                        watcher.Changed += new FileSystemEventHandler(OnChanged);
                        watcher.EnableRaisingEve`enter code here`nts = true;
                        path = dialog.FileName;
                        HandleOpen(dialog.FileName);
                    }
                }
            }

            internal void HandleOpen(string path)
            {
                FileStream f = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                StreamReader reader = new StreamReader(f);
                model.Content = reader.ReadToEnd();
                reader.Close();
            }

            private void OnChanged(object source, FileSystemEventArgs e)
            {
                if (this.Box.Dispatcher.CheckAccess())
                {
                    try
                    {
                        FileStream f = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                        StreamReader reader = new StreamReader(f);
                        model.Content = reader.ReadToEnd();
                        this.Box.CaretIndex = model.Cursor;
                        reader.Close();
                        Console.WriteLine("read:" + count2++);
                    }
                    catch (IOException x)
                    {
                        Console.WriteLine(x.Message);
                    }
                }
                else
                {
                    this.Box.Dispatcher.Invoke(
                    new updateContent(OnChanged), source, e);
                }
            }

            private void ContentChanged(object sender, TextChangedEventArgs e)
            {
                FileStream f = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                StreamWriter writer = new StreamWriter(f);
                writer.Write(this.Box.Text);
                model.Cursor = this.Box.SelectionStart;
                model.Content = this.Box.Text;
                writer.Close();
                Console.WriteLine("write:"+count++);
            }

            public delegate void updateContent(object source, FileSystemEventArgs e);
        }
    }

2 个答案:

答案 0 :(得分:3)

我明白了......它与我的应用程序的工作方式有关。通过将EnableRaisingEvents标志设置为false并稍后返回true来解决此问题。

答案 1 :(得分:1)

我认为你的问题的实际答案包含在这篇文章中: FileSystemWatcher Changed event is raised twice

总结一下,这是FileSystemWatcher类中的一个错误。保存过程通常分批进行。你的FileSystemWatcher班级会全力以赴。