Friday, April 13, 2012 4:06 PM
Here's the situation (VB in VS2010):
I have a filesystemwatcher waiting for .jpg files to arrive from the camera.
The file system watcher invokes a delegate (it works but is that the proper way to handle it?) sub that does a couple of things:
1. Adds the just arrived file name to a table
2. Attempts to display the image in a picturebox on the form. When it displays the image it imprints some information on the image that the user has entered on the screen. I use a file stream to retrieve the image.
The problem is, I'm getting a file in use orror on the "Img = System.Drawing.Image.FromStream(fs)" statement. Not all the time. Sometimes I can add three or four files to the folder in rapid succession with no issue. But then out of the blue the error will occur. Sometimes it's there to stay but other times it will go away and allow me to add more files.
I asume that I've got a timing issue going on but I don't understand how to get rid of it.
More on the use of the delegate. I was having a lot of trouble getting this to work at all and then ran into a cross-threading error so I figured the FSW must be out on its own thread. Adding the delegate got things running well until I ran into this.
As usual, thanks for any help and advice!
Friday, April 13, 2012 4:23 PM
yes, the delegate is needed, since the fsw is running asynchronously (which yes, indicates its on its own thread).
its not the most elegant solution, but you can try opening the file for write access, and see if it throws an IOException. if it does, then pause your thread for 100ms and then try again.
not pretty, but it should work.
Friday, April 13, 2012 5:24 PMA delegate will hide the cross thread error, but it doesn't correct the problem. You have to use Invoke or BeginInvoke.