Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2004, 10:37 AM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default file error ...being used by another process

I have a VB.NET application that watches a directory for files to consume. Another, off the shelf, application drops files into this directory to be consumed by my application. Sometimes the provider app does not let go of the file before my app trys to use it and I get a "can't access myfile.txt because it is being used by another process" error. I am currently handling this with an api call to sleep(time in ms) but would prefer a more robust solution.

Is there a way to test a file to see if it is locked by another process? I have searched high and low and haven't found anything. I beleive there must be a way to test for such a common and simple thing but I have obviously missed it.

Thanks,
Rick

 
Old December 16th, 2004, 10:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there...

if you are receiving that error.. why not just trap it???? you are trying to open it, so you can know if it is being use...

HTH

Gonzalo
 
Old December 16th, 2004, 11:10 AM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the quick reply gbianchi.

Yes, that's what it looks like i'll have to do but I hate it. :(
I would rather test for the condition but it looks like there is no way.

I tend to be a little obssesive over stuff like this. My problem, right? :D

Thanks again for your time,
Rick

 
Old December 16th, 2004, 12:46 PM
Registered User
 
Join Date: Nov 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I agree that the most elegant way to handle your situation is to trap the "error" condition. Error trapping is in fact testing for a condition a Try...Catch (with appropriate Case statements) is a very acceptable way to test and handle exception conditions that are outside the bounds of "mainline processes". They are not always errors but exceptions to mainline processing. This is why they are now correctly label as Exceptions not Errors. Elegant and well-developed exception handling logic is just as logically valid as the mainline logic of an application. (Just wanted to insert my two cents.)

 
Old December 16th, 2004, 12:52 PM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply redbudval.

Exactly! I was looking or a more elegant/graceful manner in which to handle this.

Your point is well taken:
"Elegant and well-developed exception handling logic is just as logically valid as the mainline logic of an application."

Thanks,
Rick

 
Old January 11th, 2005, 10:55 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

redbudval-

While I agree with most of what you say, consider this:

Exception catching is an expensive process. IMO, exception catching should be left for truly exceptional conditions. In Rick's case, he's fully expecting a file to have a lock on it so he could test for this expected condition within a standard branching structure.
 
Old January 11th, 2005, 11:56 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

To make things more readable—if a little more spaghetti-like—you could 'test' the file in a function, hiding the multiple lines of code that are involved in that way.
Code:
    Do Until Ready Or i >= SomeReasonableValue
        If FileIsReady(<Path & File>) Then
            i += 1
            Ready = True
        End If
    Loop

    ProcessFile
    Then write a function that tries to access the file, returning True if that succeeds, False if not.





Similar Threads
Thread Thread Starter Forum Replies Last Post
The process cannot access the file... DolphinBay ASP.NET 1.0 and 1.1 Professional 9 May 23rd, 2014 04:38 AM
The process cannot access the file nelly78 .NET Framework 2.0 3 July 22nd, 2008 06:59 AM
The process cannot access the file while publishin madhu15583 ASP.NET 1.0 and 1.1 Professional 0 June 19th, 2008 01:58 AM
[Resolved] Error: File used by another process snufse .NET Framework 2.0 2 February 18th, 2008 09:20 AM
DTS text file export - Batch Process Tim Barbour SQL Server DTS 1 November 23rd, 2004 11:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.