Results 1 to 11 of 11

Thread: Got suggestions? - Windows service locking files

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Got suggestions?

    Did you close the files after you opened them?

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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?

  5. #5
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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))

  6. #6

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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?

  7. #7
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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.

  8. #8

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  9. #9
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Got suggestions? - Windows service locking files

    Still not moving, I'm starting to get bed sores.

  10. #10
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Got suggestions? - Windows service locking files

    What is the exact error message u are getting plz?
    I don't live here any more.

  11. #11

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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
  •  



Click Here to Expand Forum to Full Width