FileSystemWatcher Changed event is raised twice

前端 未结 30 3854
死守一世寂寞
死守一世寂寞 2020-11-22 06:01

I have an application where I am looking for a text file and if there are any changes made to the file I am using the OnChanged eventhandler to handle the event

30条回答
  •  忘掉有多难
    2020-11-22 06:14

    I have a very quick and simple workaround here, it does work for me, and no matter the event would be triggered once or twice or more times occasionally, check it out:

    private int fireCount = 0;
    private void inputFileWatcher_Changed(object sender, FileSystemEventArgs e)
        {
           fireCount++;
           if (fireCount == 1)
            {
                MessageBox.Show("Fired only once!!");
                dowork();
            }
            else
            {
                fireCount = 0;
            }
        }
    }
    

提交回复
热议问题