|
-
Jul 17th, 2006, 04:00 PM
#1
Got suggestions? - Windows service locking files
I've created a windows service that uses a timer and an event that fires every x minutes (where x is a variable and is configurable).
The service works quite simply: It looks at a folder, enumerates all the files in that folder and every folder beneath it. It then does some processing on them (read only processing) and once that's done, it's supposed to delete all of them.
But that doesn't happen, because the service has a lock on the files.
I know I've overlooked something simple, so what should I be doing in order to delete those files?
Last edited by mendhak; Jul 18th, 2006 at 02:51 AM.
-
Jul 17th, 2006, 04:02 PM
#2
Re: Got suggestions?
Did you close the files after you opened them?
-
Jul 17th, 2006, 04:26 PM
#3
Re: Got suggestions?
Well, the read operation I open it in a binary stream and copy that into a byte array. And then I close the binary stream. Do you want to see c0d? If so, then wait... for about half a day until I get into work. Don't move away from that screen.
-
Jul 18th, 2006, 02:58 AM
#4
Re: Got suggestions? - Windows service locking files
In a class library that my service calls:
Code:
System.Drawing.Image img;
img = System.Drawing.Image.FromFile(alAllFiles[j].ToString());
Where alAllFiles is an ArrayList that contains the paths to the files.
I then:
Code:
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms,img.RawFormat);
byte[] bytImg = ms.GetBuffer();
ms.Close();
ms = null;
So I've got it in the byte array, I'm happy and now need to be able to delete the files. Suggestions? Ideas? Taunts and Jeers?
-
Jul 18th, 2006, 08:38 AM
#5
Re: Got suggestions? - Windows service locking files
Code:
private void Form1_Load(object sender, System.EventArgs e)
{
string[] alAllFiles = System.IO.Directory.GetFiles("C:\\Pic", "*.bmp");
byte[] byt;
for (int j = 0; j < alAllFiles.Length; j++)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Drawing.Image img= System.Drawing.Image.FromFile(alAllFiles[j].ToString());;
img.Save(ms,img.RawFormat);
img.Dispose();
byt = new byte[ms.Length-1];
ms.BeginRead(byt,0,Convert.ToInt32(ms.Length-1),new AsyncCallback(DeleteFile),alAllFiles[j].ToString());
ms.Close();
ms = null;
}
}
private void DeleteFile (IAsyncResult result)
{
System.IO.File.Delete(result.AsyncState.ToString());
}
You had left out "img.Dispose();"
(Note to anyone searching forums: This code is just to make it work, it's not optimized for resources (you could reuse the memorystream))
-
Jul 18th, 2006, 11:24 AM
#6
Re: Got suggestions? - Windows service locking files
That's not it either. 
I've added the img.dispose(), when that didn't work, I further set img to null. Now neither the Image object nor the MemoryStream object are to be present at the end of the loading, but the lock still persists.
Is there anything else I should check for?
-
Jul 18th, 2006, 12:01 PM
#7
Re: Got suggestions? - Windows service locking files
I tested the code with that snippet, it ran perfectly. Did you try the asynccallback? maybe it's still reading into the buffer when it attempts to delete?
(On Another note: I had received the same "another process" error without the async and the dispose. So it should work?)
Last edited by sevenhalo; Jul 18th, 2006 at 12:06 PM.
-
Jul 18th, 2006, 12:21 PM
#8
Re: Got suggestions? - Windows service locking files
No, haven't tried that. In fact, this is all a single threaded service... load files, process, delete. I'll try with async, but was hoping to avoid it.
I'll get back to this thread in half a day or a day. As always, don't move.
-
Jul 26th, 2006, 01:13 PM
#9
Re: Got suggestions? - Windows service locking files
Still not moving, I'm starting to get bed sores.
-
Jul 27th, 2006, 06:44 AM
#10
Re: Got suggestions? - Windows service locking files
What is the exact error message u are getting plz?
I don't live here any more.
-
Jul 29th, 2006, 05:17 PM
#11
Re: Got suggestions? - Windows service locking files
I don't remember the message now, as I've moved on from this problem like women move away from me. I'll bump this thread up if the error comes back to bite me.
7, you may move now... although that was quite a disturbing revelation that you were in bed the entire time, accept my apillowgies for making you wait this long. I do appreciate it and I'm not sheeting you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|