×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Reading picture IO Exception

Reading picture IO Exception

Reading picture IO Exception

(OP)
Hi,
I created a thread that runs continously that reads a picture from a folder. this picture is being read 50times per second.

I have a camera where the software is set on trigger mode(50ms) it constantly takes a picture and reaplces the old one in the same location.

while i am reading this picture i am getting an IOexception handle: the process cannot access file because it is being used by another process.

My code:
//in the thread
FileStream stream = new FileStream("Filename",filemode.open,fileaccess.read);
Bitmap bm = (Bitmap)Image.FromStream(stream);
stream.Close();

is filestream not the way to go?

//  

RE: Reading picture IO Exception

how are you continuously reading the image? with a filesystemwatcher, or with a loop? if it's the latter, remove the loop and use a filesystemwatcher.

2nd, use a using or try/finally block to properly clean up the resources

CODE

using(var stream = new FileStream(...))
{
   return Image.FromStream(stream);
}

another approach is to unqiuely rename the file. then you can work with it and reduce the chance of IO exceptions.

CODE

var destination = string.Format("{0}.bmp", Guid.NewGuid());
File.Move(source, destination);
if you still have problems. it could be due to the fact that you are attempting to read and write to an image file 50 times a second.

Jason Meckley
Programmer

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

RE: Reading picture IO Exception

(OP)
Icreated at thread:
ThreadPool.setMaxThread(50,50);
for(int i=0;i<50;i++)
{
threadpool.queueUserWorkItem(new WaitCallback(doWork));
}

void Dowork()
{
 while(true)
 {
   //i even tried you 2nd method with File.Move
   System.IO.File.Move(sourcefile,newfilelocation);
   using(filestream stream = new     filestream(filetemp,filemode,open,fileaccess.read())
   {
     bitmap=(bitmap)Image.FromStream(stream);
     stream.close();
     //calculation of the image

   }
 }
}

the reason i use thread is that after the image is read it goes through alot of calculations to generate avalue. and i need to be able to executes this 50-100/second so i thought multi-threading wast he best way.

the image speed(frames per second) that it is taking canoot change as it needs to be fast (50images a second).will filewatcher able to keep up with this speed and if a image is replacing another iamge will filewatcher catch that?

RE: Reading picture IO Exception

try it and find out. what you currently have is a race condition where multiple threads are attempting to all lock the same file at the same time.

assuming the calculations are intensive. in this case more than a few ms. I would copy the image to a uniquely named file, then perform calculations and delete/archive the file when finished.

something else to consider is that you are getting into the realm of real-time processing (if you aren't already) and I don't think .net is designed for that. IIRC there is a disclaimer with java and .net that it cannot be used for critical operations like life support and nuclear power management because the reaction time is too slow.

the other option, if possible, is to have the camera software save each image to a unique file. once taken, you can process the image and delete/archive the file. this would prevent locks, because there would be only 1 read a write.

Jason Meckley
Programmer

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close