File locked problem in c#

A

Albert Greinöcker

Hi ng,

I have a very strange problem within my c# application:
One of the (background) Threads in my application is responsible for some
image processing tasks (like resizing, clipping, ..) on a list of files.
Therefore, some temporary files are created which should be deleted after
processing is finished.
This Thread is called several times. When I call it the first time,
everything's ok and no errors occur.
In the second call, I can observe very strange behaviour: I cannot delete
the temporary files created by this thread, but I'm sure that all image and
bitmap-objects are disposed after processing (with this method,which is, I
think more than should be necessary to release the file lock)
private static void DisposeImage(Image image)

{
image.Dispose();
image = null;
GC.Collect();
}

Did anyone observe similar behaviour? Does anyone have any ideas what could
be the reason for this annoying file locks (e.g. error in threading or
whatever..)
Or, how could a workaround look like (which means how can I unlock a file
which was created by the same thread)

thx,
Albert
 
?

=?ISO-8859-1?Q?=22Patrik_L=F6wendahl_=5BC=23_MVP=5

You should always make sure that you close the streams used to load the
images as well.

Maybe that is the problem you're experiencing?
 
A

Albert Greinöcker

Thanks for your response...

The only two ways I'm opening the images is either

Image img = Image.FromFile(filePath);

Or with a copy constructor

Are in this case any additional closing steps necessary?



"Patrik Löwendahl [C# MVP]" said:
You should always make sure that you close the streams used to load the
images as well.

Maybe that is the problem you're experiencing?

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net

Hi ng,

I have a very strange problem within my c# application:
One of the (background) Threads in my application is responsible for some
image processing tasks (like resizing, clipping, ..) on a list of files.
Therefore, some temporary files are created which should be deleted after
processing is finished.
This Thread is called several times. When I call it the first time,
everything's ok and no errors occur.
In the second call, I can observe very strange behaviour: I cannot delete
the temporary files created by this thread, but I'm sure that all image
and bitmap-objects are disposed after processing (with this method,which
is, I think more than should be necessary to release the file lock)
private static void DisposeImage(Image image)

{
image.Dispose();
image = null;
GC.Collect();
}

Did anyone observe similar behaviour? Does anyone have any ideas what
could be the reason for this annoying file locks (e.g. error in threading
or whatever..)
Or, how could a workaround look like (which means how can I unlock a file
which was created by the same thread)

thx,
Albert
 
?

=?ISO-8859-1?Q?=22Patrik_L=F6wendahl_=5BC=23_MVP=5

I'm not a 100% for the implementation of file reading in GDI.

But I would try not to use a filename but instead use a filestream which
you could explicitly close.


--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.
Thanks for your response...

The only two ways I'm opening the images is either

Image img = Image.FromFile(filePath);

Or with a copy constructor

Are in this case any additional closing steps necessary?



You should always make sure that you close the streams used to load the
images as well.

Maybe that is the problem you're experiencing?

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net

Hi ng,

I have a very strange problem within my c# application:
One of the (background) Threads in my application is responsible for some
image processing tasks (like resizing, clipping, ..) on a list of files.
Therefore, some temporary files are created which should be deleted after
processing is finished.
This Thread is called several times. When I call it the first time,
everything's ok and no errors occur.
In the second call, I can observe very strange behaviour: I cannot delete
the temporary files created by this thread, but I'm sure that all image
and bitmap-objects are disposed after processing (with this method,which
is, I think more than should be necessary to release the file lock)
private static void DisposeImage(Image image)

{
image.Dispose();
image = null;
GC.Collect();
}

Did anyone observe similar behaviour? Does anyone have any ideas what
could be the reason for this annoying file locks (e.g. error in threading
or whatever..)
Or, how could a workaround look like (which means how can I unlock a file
which was created by the same thread)

thx,
Albert
 
A

Albert Greinöcker

I'm also not very happy with the GDI-implementation...
which alternative would you advice?

"Patrik Löwendahl [C# MVP]" said:
I'm not a 100% for the implementation of file reading in GDI.

But I would try not to use a filename but instead use a filestream which
you could explicitly close.


--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.
Thanks for your response...

The only two ways I'm opening the images is either

Image img = Image.FromFile(filePath);

Or with a copy constructor

Are in this case any additional closing steps necessary?



You should always make sure that you close the streams used to load the
images as well.

Maybe that is the problem you're experiencing?

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net


Albert Greinöcker wrote:

Hi ng,

I have a very strange problem within my c# application:
One of the (background) Threads in my application is responsible for
some image processing tasks (like resizing, clipping, ..) on a list of
files.
Therefore, some temporary files are created which should be deleted
after processing is finished.
This Thread is called several times. When I call it the first time,
everything's ok and no errors occur.
In the second call, I can observe very strange behaviour: I cannot
delete the temporary files created by this thread, but I'm sure that all
image and bitmap-objects are disposed after processing (with this
method,which is, I think more than should be necessary to release the
file lock)
private static void DisposeImage(Image image)

{
image.Dispose();
image = null;
GC.Collect();
}

Did anyone observe similar behaviour? Does anyone have any ideas what
could be the reason for this annoying file locks (e.g. error in
threading or whatever..)
Or, how could a workaround look like (which means how can I unlock a
file which was created by the same thread)

thx,
Albert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top