N
Nony Buz
My objective is simply: Notify a form when a image file has been
created in a directory. First there is a basic interface for the
callback function:
public interface INewImageNotify
{
void notify_NewImage(string imageName);
}
Then there is the delegate:
delegate void DelegateNewImage(string imageName);
The form implements this interface. Then there is a separate
class that manages the FileSystemWatcher. The form creates one of
these class, the constructor looks like this:
public ImageMonitor(string soureDir, string filter,
ImageSelectorForm form)
{
m_form = form;
m_watcher = new FileSystemWatcher(sourceDir, filter);
m_watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite | NotifyFilters.FileName |
NotifyFilters.DirectoryName;
m_watcher.Created += new FileSystemEventHandler(this. OnCreate);
m_watcher.EnableRaisingEvents = true;
}
then this class implements OnCreate(...):
private void OnCreate(object source, FileSystemEventArgs e)
{
FileInfo fileInfo = new FileInfo(e.FullPath);
if( String.Compare( fileInfo.Extension, ".jpg") == 0 )
{
notify_NewImage(e.FullPath);
}
}
And the class also implements the INewImageNotify interface:
public void notify_NewImage(string imageName)
{
if ((m_form != null) && (m_form.Visible))
{
try
{
object[] args = new object[1];
args[0] = imageName;
m_form.Invoke( new DelegateNewImage( m_form.notify_NewImage), args);
}
catch( System.ArgumentException e)
{
Debug.WriteLine( e.ToString());
}
}
}
The first time this notify_NewImage() gets called, all is well.
The second time through, the Invoke throws
System.ArgumentException. I don't get it! Why does it work once?
Why isn't it working at all after that? Is there a better
approach?
created in a directory. First there is a basic interface for the
callback function:
public interface INewImageNotify
{
void notify_NewImage(string imageName);
}
Then there is the delegate:
delegate void DelegateNewImage(string imageName);
The form implements this interface. Then there is a separate
class that manages the FileSystemWatcher. The form creates one of
these class, the constructor looks like this:
public ImageMonitor(string soureDir, string filter,
ImageSelectorForm form)
{
m_form = form;
m_watcher = new FileSystemWatcher(sourceDir, filter);
m_watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite | NotifyFilters.FileName |
NotifyFilters.DirectoryName;
m_watcher.Created += new FileSystemEventHandler(this. OnCreate);
m_watcher.EnableRaisingEvents = true;
}
then this class implements OnCreate(...):
private void OnCreate(object source, FileSystemEventArgs e)
{
FileInfo fileInfo = new FileInfo(e.FullPath);
if( String.Compare( fileInfo.Extension, ".jpg") == 0 )
{
notify_NewImage(e.FullPath);
}
}
And the class also implements the INewImageNotify interface:
public void notify_NewImage(string imageName)
{
if ((m_form != null) && (m_form.Visible))
{
try
{
object[] args = new object[1];
args[0] = imageName;
m_form.Invoke( new DelegateNewImage( m_form.notify_NewImage), args);
}
catch( System.ArgumentException e)
{
Debug.WriteLine( e.ToString());
}
}
}
The first time this notify_NewImage() gets called, all is well.
The second time through, the Invoke throws
System.ArgumentException. I don't get it! Why does it work once?
Why isn't it working at all after that? Is there a better
approach?