Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading picture IO Exception

Status
Not open for further replies.

jl3574

Programmer
Jun 5, 2003
76
CA
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?

//
 
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
faq732-7259
 
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?
 
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
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top