473,804 Members | 3,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting System.Argument Exception when invoking a delegate

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 DelegateNewImag e(string imageName);

The form implements this interface. Then there is a separate
class that manages the FileSystemWatch er. The form creates one of
these class, the constructor looks like this:

public ImageMonitor(st ring soureDir, string filter,
ImageSelectorFo rm form)
{
m_form = form;
m_watcher = new FileSystemWatch er(sourceDir, filter);

m_watcher.Notif yFilter = NotifyFilters.L astAccess |
NotifyFilters.L astWrite | NotifyFilters.F ileName |
NotifyFilters.D irectoryName;

m_watcher.Creat ed += new FileSystemEvent Handler(this. OnCreate);
m_watcher.Enabl eRaisingEvents = true;
}

then this class implements OnCreate(...):

private void OnCreate(object source, FileSystemEvent Args e)
{
FileInfo fileInfo = new FileInfo(e.Full Path);
if( String.Compare( fileInfo.Extens ion, ".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 DelegateNewImag e( m_form.notify_N ewImage), args);
}
catch( System.Argument Exception e)
{
Debug.WriteLine ( e.ToString());
}
}
}

The first time this notify_NewImage () gets called, all is well.
The second time through, the Invoke throws
System.Argument Exception. I don't get it! Why does it work once?
Why isn't it working at all after that? Is there a better
approach?
Nov 17 '05 #1
2 2493
I found the problem, but don't know the fix. The problem is that
FileSystemWatch er is too fast. It is notifying as soon as the
file is created, in my case, before the copy is complete. Thus,
in the form where it is loaded, the load fails. How do I find out
when the file is completely copied?

On 2005-04-05, Nony Buz <no**@dwtty.com > wrote:
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 DelegateNewImag e(string imageName);

The form implements this interface. Then there is a separate
class that manages the FileSystemWatch er. The form creates one of
these class, the constructor looks like this:

public ImageMonitor(st ring soureDir, string filter,
ImageSelectorFo rm form)
{
m_form = form;
m_watcher = new FileSystemWatch er(sourceDir, filter);

m_watcher.Notif yFilter = NotifyFilters.L astAccess |
NotifyFilters.L astWrite | NotifyFilters.F ileName |
NotifyFilters.D irectoryName;

m_watcher.Creat ed += new FileSystemEvent Handler(this. OnCreate);
m_watcher.Enabl eRaisingEvents = true;
}

then this class implements OnCreate(...):

private void OnCreate(object source, FileSystemEvent Args e)
{
FileInfo fileInfo = new FileInfo(e.Full Path);
if( String.Compare( fileInfo.Extens ion, ".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 DelegateNewImag e( m_form.notify_N ewImage), args);
}
catch( System.Argument Exception e)
{
Debug.WriteLine ( e.ToString());
}
}
}

The first time this notify_NewImage () gets called, all is well.
The second time through, the Invoke throws
System.Argument Exception. I don't get it! Why does it work once?
Why isn't it working at all after that? Is there a better
approach?

Nov 17 '05 #2
Nony Buz wrote:
I found the problem, but don't know the fix. The problem is that
FileSystemWatch er is too fast. It is notifying as soon as the
file is created, in my case, before the copy is complete. Thus,
in the form where it is loaded, the load fails. How do I find out
when the file is completely copied?
Put the copying routine in a separate thread(so that it won't freeze the app while waiting),
and in it check for ability to copy file periodically, say, once every second or so...
Put file cpying func in try{}catch{} so it won't fire every time it fails.

Something like this:

void ThreadFunc()
{
bool bOk = false;
while (!bOk)
{
try
{
Fiel.Copy(strFi leName, strNewFileName) ;
bOk = true;
}
catch(IOExcepti on)
{Thread.Sleep(1 000)); // Not correct - don't remember how to call sleep here, but yo u get the idea
}
}
Or to improve, create a thread class and include a callback/delegate function which will fire when
thread is done.

I'm not a "pro" with threads, so maybe you know better way of dealing with threads here, but the
idea is as above. I remember using it myself in a project a year ago - worked perfect

Hope it helps,
Andrey

On 2005-04-05, Nony Buz <no**@dwtty.com > wrote:
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 DelegateNewImag e(string imageName);

The form implements this interface. Then there is a separate
class that manages the FileSystemWatch er. The form creates one of
these class, the constructor looks like this:

public ImageMonitor(st ring soureDir, string filter,
ImageSelector Form form)
{
m_form = form;
m_watcher = new FileSystemWatch er(sourceDir, filter);

m_watcher.Notif yFilter = NotifyFilters.L astAccess |
NotifyFilters.L astWrite | NotifyFilters.F ileName |
NotifyFilters.D irectoryName;

m_watcher.Creat ed += new FileSystemEvent Handler(this. OnCreate);
m_watcher.Enabl eRaisingEvents = true;
}

then this class implements OnCreate(...):

private void OnCreate(object source, FileSystemEvent Args e)
{
FileInfo fileInfo = new FileInfo(e.Full Path);
if( String.Compare( fileInfo.Extens ion, ".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 DelegateNewImag e( m_form.notify_N ewImage), args);
}
catch( System.Argument Exception e)
{
Debug.WriteLine ( e.ToString());
}
}
}

The first time this notify_NewImage () gets called, all is well.
The second time through, the Invoke throws
System.Argume ntException. I don't get it! Why does it work once?
Why isn't it working at all after that? Is there a better
approach?

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1739
by: Jeff Van Epps | last post by:
We've been unable to get events working going from C# to VJ++. We think that the C# component is being exposed properly as a ConnectionPoint, and the Advise() from the VJ++ side seems to be working, because the delegate on the C# side has 1 item in its invocation list after the Advise(). However when we try to make the callback, we get: System.NullReferenceException: Object reference not set to an instance of an object at...
5
13507
by: Rik Hemsley | last post by:
How does one go about getting a pointer to method as an IntPtr? Example: public class A { public void X() { Y(Z); }
18
4882
by: Atara | last post by:
In my apllication I use the following code: '-- My Code: Public Shared Function strDate2Date(ByVal strDate As String) As System.DateTime Dim isOk As Boolean = False If (strDate Is Nothing) Then isOk = False ElseIf Not (strDate.Length() = 6) Then isOk = False
4
5297
by: Ty Salistean | last post by:
So, here is a wierd question that we have been discussing for a bit now. Does an event fire even though nothing is subscribed to listen to the event? For instance, does the Click event of a button fire even though nothing is subscribed to listen to the event. The answer is NO in a traditional Publisher/Subscriber design pattern (at least I think it is). I have stated that if nothing is subscribed, then the event never gets
6
2850
by: =?Utf-8?B?QkJN?= | last post by:
Hi, I have an app that is crashing due to a System.ArgumentException. At this point it's just a simple app to test some basic object values. The main app is a Windows App that looks like this. Public Class DesTestForm Private Sub DesTestForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
2
2500
by: james | last post by:
I have a console app with the code: public static void Main(String args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( delegate(object sender, UnhandledExceptionEventArgs e) { using (ErrorReportProxy proxy = new ErrorReportProxy()) {
33
11869
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon (NT_AUTHORITY\SYSTEM). I understand that when the service starts, no user may be logged in, but that's ok, as the app I am monitoring can only be run by a logged in user. Do I need to use WMI to get the user context of Explorer.exe or is there a...
3
1948
by: ohmmega | last post by:
since its common knowledge that it's not possible to hide a tabcontrol tab, i made a collection for removed pages including their tabcontrols. removing works pretty fine, but whenever i try to add a page (timer triggered) i get stuck: System.ArgumentException the message is like: controls, created for a thread cannot superordinate controls from another thread.
4
5722
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a main thread an another worker thread. The main Thread creates another thread and waits for the threads signal to continue the main thread. Everything works inside a ModalDialog and everyting is secured by Invoke/BeginInvoke, and synchronisation primitves like WaitHandles, Evetns, Semaphores, etc... All works good and with no race conditions or locks. I have a System.Windows.Forms.Timer in the main
0
10567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.