Home All Groups Group Topic Archive Search About
Author
13 May 2009 7:08 AM
Tony WONG
my video program keeps writing videos to a folder

i wish to write a program to move "completed" videos to another folder

how can i detect the video file completed (not locked?)

Thanks a lot.

tony

Author
13 May 2009 8:53 AM
Andreas Johansson
You can use the FileSystemWatcher class.
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

Assuming that the application writing the file has the proper read/write
locks you can try to open the video file for exclusive read/write in the
change event of the FileSystemWatcher and then try to open it with exclusive
read/write locks. If it succeeds you know the file is complete.
Author
13 May 2009 9:54 AM
Johnny_Jörgensen
The filesystemwatcher will not work in this case.

It's file created event is fired when the file creation starts - not when it
finishes.

(A fact that's really irritating in my opinion, btw).

It's a better solution to simply try to open it as Andreas also suggest.

Good luck,
Johnny J.


Show quoteHide quote
"Andreas Johansson" <afjohanssonREM***@hotmail.com> skrev i meddelandet
news:%23IxwJh60JHA.4880@TK2MSFTNGP03.phx.gbl...
> You can use the FileSystemWatcher class.
> http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
>
> Assuming that the application writing the file has the proper read/write
> locks you can try to open the video file for exclusive read/write in the
> change event of the FileSystemWatcher and then try to open it with
> exclusive read/write locks. If it succeeds you know the file is complete.
>
>
Author
13 May 2009 11:35 AM
Mike
Johnny,

You can use FileSystemWatcher (ReadDirectoryChangesW) to detect when
the file is closed.

When the event comes in, you attempt to open it, which will be locked
and you will get error 5 or 32.  From here you have choices, either
you  prepare a worker thread to keep trying to open or do it within
the IOCP event for the close event which will happen.  That can get
very complicated but that depends if there a lot of  events. If the OP
is just looking for one, then it will be fine.  TIP: Look at the file
size. When not zero, it is closed.

Pseudo code:

     if FileSize is zero
       do forever
          open file
          if error 5 or 32 then
             sleep x milliseconds
             continue
          end if
          copy/move file
          close
          exit loop
       loop

--

Johnny Jörgensen wrote:
Show quoteHide quote
> The filesystemwatcher will not work in this case.
>
> It's file created event is fired when the file creation starts - not
> when it finishes.
>
> (A fact that's really irritating in my opinion, btw).
>
> It's a better solution to simply try to open it as Andreas also suggest.
>
> Good luck,
> Johnny J.
>
>
> "Andreas Johansson" <afjohanssonREM***@hotmail.com> skrev i meddelandet
> news:%23IxwJh60JHA.4880@TK2MSFTNGP03.phx.gbl...
>> You can use the FileSystemWatcher class.
>> http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
>>
>> Assuming that the application writing the file has the proper
>> read/write locks you can try to open the video file for exclusive
>> read/write in the change event of the FileSystemWatcher and then try
>> to open it with exclusive read/write locks. If it succeeds you know
>> the file is complete.
>>
>>
>
Author
13 May 2009 11:06 AM
Andreas Johansson
The purpose to use the FileSystemWatcher would be to get some kind of event
to know when it might be possible to open it. It would work to use a timer
or user interaction like clicking a button also.

I prefer to work with events as it usually ends up using less resources (cpu
time).
Author
13 May 2009 11:30 AM
Tony WONG
Thanks a lot.


"Andreas Johansson" <afjohanssonREM***@hotmail.com>
???????:89CA7BD8-2CB4-4091-AA89-51E29B3FD***@microsoft.com...
Show quoteHide quote
> The purpose to use the FileSystemWatcher would be to get some kind of
> event to know when it might be possible to open it. It would work to use a
> timer or user interaction like clicking a button also.
>
> I prefer to work with events as it usually ends up using less resources
> (cpu time).
Author
13 May 2009 11:31 AM
Armin Zingler
Andreas Johansson wrote:
> The purpose to use the FileSystemWatcher would be to get some kind of
> event to know when it might be possible to open it. It would work to
> use a timer or user interaction like clicking a button also.
>
> I prefer to work with events as it usually ends up using less
> resources (cpu time).

It doesn't matter which event you choose. In any case he will have to try
opening the file in order to know if it's still locked. There is no other
way.


Armin
Author
13 May 2009 11:40 AM
Armin Zingler
Armin Zingler wrote:
> Andreas Johansson wrote:
>> The purpose to use the FileSystemWatcher would be to get some kind of
>> event to know when it might be possible to open it. It would work to
>> use a timer or user interaction like clicking a button also.
>>
>> I prefer to work with events as it usually ends up using less
>> resources (cpu time).
>
> It doesn't matter which event you choose. In any case he will have to
> try opening the file in order to know if it's still locked. There is
> no other way.

Andreas, this was no disagreement (it could be read as if). Just an
addition.


Armin