469,286 Members | 2,442 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

File locked problem in c#

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

Jul 21 '05 #1
4 13431
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

Jul 21 '05 #2
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]"" <pa******************************@home.se>
schrieb im Newsbeitrag news:OX**************@tk2msftngp13.phx.gbl...
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


Jul 21 '05 #3
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.

Albert Grein÷cker wrote:
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]"" <pa******************************@home.se>
schrieb im Newsbeitrag news:OX**************@tk2msftngp13.phx.gbl...
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


Jul 21 '05 #4
I'm also not very happy with the GDI-implementation...
which alternative would you advice?

""Patrik L÷wendahl [C# MVP]"" <pa******************************@home.se>
schrieb im Newsbeitrag news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Albert Grein÷cker wrote:
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]"" <pa******************************@home.se>
schrieb im Newsbeitrag news:OX**************@tk2msftngp13.phx.gbl...
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


Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Pekka Niiranen | last post: by
5 posts views Thread by Fabio R. | last post: by
4 posts views Thread by hkappleorange | last post: by
5 posts views Thread by cmay | last post: by
6 posts views Thread by elake | last post: by
reply views Thread by Dmitry Teslenko | last post: by
reply views Thread by Gabriel Genellina | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.