web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How To detect file lock in batch process

(0) ShareShare
ReportReport
Posted on by 334

Hi, 

We would like to detect if the file is locked before we move the file

We use code like :


if (systemio::filelock(filename))
     ok = checkedfail("filelock error");
else
      info("file is not locked");


The most files is no problem and showing "file is not locked"
However, for some files, I get the error message

unhandled clr exception occurred
Exception has been thrown by the target of an invocation.
The process cannot access the file XXX because it is being used by another process.


I try to write a job to open a file, and use this filelock() function to test it and
It did show the message("file is locked");

so i don't know how to re-produced it in test environment for this exception and how to modify the code
Can you experts provide me some advises it would be appreciated.!

Quatrani

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    236,534 Most Valuable Professional on at
    RE: How To detect file lock in batch process

    What code do you have in systemio::filelock()?

  • JCHI Profile Picture
    334 on at
    RE: How To detect file lock in batch process

    The filelocked method is like this:

    public static boolean fileLocked(Filename _filename)

    {

       System.Exception          ex;

       System.IO.FileStream    fileStream;

       boolean locked = false;

       ;

       try

       {

           new FileIOPermission(_filename, 'rw').assert();

           fileStream = System.IO.File::Open(_filename, System.IO.FileMode::Open, System.IO.FileAccess::ReadWrite, System.IO.FileShare::None);

           CodeAccessPermission::revertAssert();

       }

       catch (Exception::CLRError)

       {

           locked  = true;

       }

       return locked;

    }

  • Martin Dráb Profile Picture
    236,534 Most Valuable Professional on at
    RE: How To detect file lock in batch process

    Please verify whether you get the exception here (and your exception-handling code is ignored) and not at some other place.

    If it's the case, isn't the reason why the exception isn't handled that your code runs in a transaction?

    By the way, there may be other reasons why Open() throws an exception, such as if the file doesn't exist. The fact that your method claims that the file is locked even in such cases sounds wrong.

  • JCHI Profile Picture
    334 on at
    RE: How To detect file lock in batch process

    Hi , Martin:

    Thank you so much so the reply.

    For the first point.  I check the code again and think the exception is from the filelocked() method.

    and these code is not inside transaction block.  I'll try to do some more testing.

    About the second point, I think I should add a statement like

    if (! _filename || ! WinAPIServer::fileExists(_filename))

           return checkFailed("file not existed");

    Quatrani

  • Verified answer
    Martin Dráb Profile Picture
    236,534 Most Valuable Professional on at
    RE: How To detect file lock in batch process

    Please use the debugger to be 100% sure that you're looking at the right piece of code. When doing that, verify the TTS level. There may be a transaction even if you're not aware of it.

    Even if you add a check whether the file exists or not, the fundamental design problem will be still there: your code claims that any exception thrown when accessing the file is caused by a lock, which clearly isn't true. Another example is that the user isn't authorized to do it.

  • JCHI Profile Picture
    334 on at
    RE: How To detect file lock in batch process

    Hi, Martin:

    Thank you again for the precious advise.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#2
Community Member Profile Picture

Community Member 2

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans