进程在 OnChanged 事件中启动了两次
Process is started twice in the OnChanged event
我已经创建了一个程序来使用 filewatcher 监视记事本文件..每当文件中的文本发生变化时..我制作了一个 .exe 程序 运行....exe 程序也 运行s 正确..但它 运行s 两次...我搜索了 Whosebug 和其他网站..但我不明白我哪里出错了..Frnds Plz 帮助我清除错误..我知道这个问题已经被问过很多次了。但是因为我是这个 c# 的新手,所以我无法理解。请帮助我 frnds ..
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();
}
}
}
}
您有密码
fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
您可以重复向事件添加相同的事件处理程序..所以是的,它可以执行 20 次..
只需要设置运行代码一次,所以只加一次。如果您停止触发事件,则事件处理程序不会擦除。所以设置事件处理程序一次,而不是每次都在你的 filewatcher 方法中,它会重复添加相同的处理程序并产生多个响应。
因此请在此处更改您的代码:
public Form1()
{
InitializeComponent();
fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
}
并将其从 filewatcher 方法中删除。现在它会 运行 一次,当 raiseevents 设置为 true 时,根本不会。
我已经创建了一个程序来使用 filewatcher 监视记事本文件..每当文件中的文本发生变化时..我制作了一个 .exe 程序 运行....exe 程序也 运行s 正确..但它 运行s 两次...我搜索了 Whosebug 和其他网站..但我不明白我哪里出错了..Frnds Plz 帮助我清除错误..我知道这个问题已经被问过很多次了。但是因为我是这个 c# 的新手,所以我无法理解。请帮助我 frnds ..
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();
}
}
}
}
您有密码
fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
您可以重复向事件添加相同的事件处理程序..所以是的,它可以执行 20 次..
只需要设置运行代码一次,所以只加一次。如果您停止触发事件,则事件处理程序不会擦除。所以设置事件处理程序一次,而不是每次都在你的 filewatcher 方法中,它会重复添加相同的处理程序并产生多个响应。
因此请在此处更改您的代码:
public Form1()
{
InitializeComponent();
fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
}
并将其从 filewatcher 方法中删除。现在它会 运行 一次,当 raiseevents 设置为 true 时,根本不会。