Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am able to close this form from within the ButtonOnClick method but when I try the same from FileSystemWatcher OnChange event handler the form does not close.. Any help would be greatly appreciated.
Posted
Updated 21-Nov-15 0:38am
v3
Comments
Richard MacCutchan 21-Nov-15 4:03am    
Help with what? We have no idea what your code does.
ElexUK 21-Nov-15 6:23am    
Hi Richard, thanks for prompt reply.

There are two forms in the Windows.Forms.Application
Main Form:

Hide Expand Copy Code
using System;
using System.IO;
using System.Windows.Forms;

namespace FileSystemWatcherApp
{
public partial class MainForm : Form
{
NotificationForm nf = new NotificationForm();
FileSystemWatcher fileWatcher = new FileSystemWatcher();

public MainForm()
{
InitializeComponent();

fileWatcher.Path = "C:\\Test";
fileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
fileWatcher.Filter = "*.*";

// Add event handlers.
fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
fileWatcher.Created += new FileSystemEventHandler(OnChanged);

fileWatcher.EnableRaisingEvents = true;
}

private void OnChanged(object sender, FileSystemEventArgs e)
{
nf.Show();
}

private void button1_Click(object sender, EventArgs e)
{
nf.Show();
}

}
}

NotificationForm:

Hide Copy Code
using System;
using System.Windows.Forms;

namespace FileSystemWatcherApp
{
public partial class NotificationForm : Form
{
public NotificationForm()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
this.Close();
}

private void NotificationForm_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
}
}

Now, the problem is that when the NotificationForm is Showed by OnClick method then the timer on this form closes the form nicely, where if the NotificationForm is Showed from OnChanged method then it does not close after the time is up.
Richard MacCutchan 21-Nov-15 6:50am    
Difficult to say what is happening, but I would suggest adding some debug code to see what the sequence oof events is when the onChanged event gets fired.
BillWoodruff 21-Nov-15 6:12am    
To assist you with this, we need to see the code in the Event raised by the FileSystemWatcher, and we need to know the context that Event is firing in. Are you using Threads here ?
ElexUK 21-Nov-15 6:35am    
Hi, here is the code:

There are two forms in the Windows.Forms.Application
Main Form:
<pre lang="C#">
using System;
using System.IO;
using System.Windows.Forms;

namespace FileSystemWatcherApp
{
public partial class MainForm : Form
{
NotificationForm nf = new NotificationForm();
FileSystemWatcher fileWatcher = new FileSystemWatcher();

public MainForm()
{
InitializeComponent();

fileWatcher.Path = "C:\\Test";
fileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
fileWatcher.Filter = "*.*";

// Add event handlers.
fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
fileWatcher.Created += new FileSystemEventHandler(OnChanged);

fileWatcher.EnableRaisingEvents = true;
}

private void OnChanged(object sender, FileSystemEventArgs e)
{
nf.Show();
}

private void button1_Click(object sender, EventArgs e)
{
nf.Show();
}

}
}

</pre>

NotificationForm:

using System;
using System.Windows.Forms;

namespace FileSystemWatcherApp
{
public partial class NotificationForm : Form
{
public NotificationForm()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
this.Close();
}

private void NotificationForm_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
}
}

Now, the problem is that when the NotificationForm is Showed by OnClick method then the timer on this form closes the form nicely, where if the NotificationForm is Showed from OnChanged method then it does not close after the time is up.

1 solution

The file system watcher (FSW) is not raising it's events on the user interface thread. It does this when the SynchronizingObject property is not set and you'll need to add an assignment into the MainForm constructor.

When a FSW is creating within the Visual Studio Form's designer the required code to set the SynchronizingObject property is automatically generated. Try it and see.

Alan.
 
Share this answer
 
Comments
ElexUK 21-Nov-15 8:15am    
Thank you very much it has solved the problem.

Added the following to MainForm.Designer.cs :
private void InitializeComponent()
{
\\ fileWatcher
this.fileWatcher = new System.IO.FileSystemWatcher();
.
.
this.fileWatcher.SynchronizingObject = this;
}


private System.IO.FileSystemWatcher fileWatcher;
BillWoodruff 21-Nov-15 8:41am    
+5 Exactly what the user needed. fyi: as you may know, you can use Invoke and a MethodInvoker to manipulate UI elements from a FileSystemWatcher if you do not wish to "attach" a Synchronizing Object. However, it's not very pretty code.
Alan N 21-Nov-15 10:30am    
It often catches people out as Microsoft examples often use the console which doesn't care much about threads. The Process class, System.Timers.Timer and the EventLog also have SynchronizingObject and so it the first thing I look for when I see questions about them.
BillWoodruff 21-Nov-15 22:27pm    
And that's why you get a #5 from me :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900