c# - 事件触发触发两次

标签 c# events

我会尽力解释我的情况。

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

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

如果我输入字符,它就会工作(两个事件都会触发一次) 如果我尝试删除任何形式的字符(退格键、删除等),它就不起作用它不会删除任何字符,光标总是重置到位置 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);
        }
    }

最佳答案

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

关于c# - 事件触发触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14249427/

相关文章:

c# - 获取互斥量时如何避免竞争条件?

java - 在 Java Applet 上捕获 WACOM 平板电脑事件

java - 在 GUI 中调用 actionPerformed 时如何调用方法

java - 鼠标事件: the order of operations

c# - 即使异常将 DbNull 作为类型抛出,也无法将具有空值的 DbGeometry 与 DbNull 进行比较

c# - 使用 linq to sql 求和列

javascript - 在同级元素的单击事件中从内存中删除 Image() 对象 — JavaScript

javascript - jQuery 取消绑定(bind)范围内声明的事件

c# - 如何在运行时移除/删除图片框?

c# - 如何知道用户更改 winform 中的任何值?