Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 178795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:17:36+00:00 2026-05-11T14:17:36+00:00

BACKGROUND: I use an offset into a file and the Filestream lock/unlock menthods to

  • 0

BACKGROUND: I use an offset into a file and the Filestream lock/unlock menthods to control read/write access. I am using the following code to test if a lock is currently held on the file

try {   fs.Lock( RESERVED_BYTE, 1 );   fs.Unlock( RESERVED_BYTE, 1 );   rc = 1; } catch {    rc = 0;  } 

QUESTION:
My goal is to eliminate the try/catch block. Is there some better way to see if the lock exists?

EDIT:
Note: This question is not about if the file exists. I already know it does. It is about synchronizing write access.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:17:37+00:00Added an answer on May 11, 2026 at 2:17 pm

    You can call the LockFile Windows API function through the P/Invoke layer directly. You would use the handle returned by the SafeFileHandle property on the FileStream.

    Calling the API directly will allow you to check the return value for an error condition as opposed to resorting to catching an exception.


    Noah asks if there is any overhead in making the call to the P/Invoke layer vs a try/catch.

    The Lock file makes the same call through the P/Invoke layer and throws the exception if the call to LockFile returns 0. In your case, you aren’t throwing an exception. In the event the file is locked, you will take less time because you aren’t dealing with a stack unwind.

    The actual P/Invoke setup is around seven instructions I believe (for comparison, COM interop is about 40), but that point is moot, since your call to LockFile is doing the same thing that the managed method does (use the P/Invoke layer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I'm using Google's protobuf , and I would like to read/write several gigabytes
Is it possible/recommended to use background worker threads with the web browser control? I
I've been following a tutorial that explains how to use background for a button
I have the following piece of jQuery $(document).ready(function(){ var vH=$('#background').height(); var vW=$('#background').width(); var vT=$('#background').offset().top;
Background I use JScript (Microsoft's ECMAScript implementation) for a lot of Windows system administration
Background: I use rails g scaffold hotel name stars:integer to start fast (and insert
A little background: I use PowerShell on windows xp at work and I set
If I use @import icon/*.png; @include all-icon-sprites; I can use background-position: sprite-position($icon, foobar); to
I have to design a form with an input inside it. I use background
background: trying to use the twitter gem for ruby-on-rails. in routes: map.resources :twitter_sessions map.finalize_twitter_sessions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.