Exclusive Writes to a file.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • L

    Exclusive Writes to a file.

    Hi,

    If two applications try to write to the same file at the same time,
    an exception gets thrown. How can we handle exclusive writes to a file?

    Thanks,
    Lalasa.

  • Claire

    #2
    Re: Exclusive Writes to a file.

    Set the correct file share mode when you try to open the file


    Comment

    • Olaf Baeyens

      #3
      Re: Exclusive Writes to a file.

      > If two applications try to write to the same file at the same time,[color=blue]
      > an exception gets thrown. How can we handle exclusive writes to a file?
      >[/color]
      This is an art on its self.

      Best is to do retries with a randomized interval.
      For example try to open the file with 10 retries but wait about 0.5 second
      between each trial of opening.
      But this means that the time of the open and close of that file should be
      less that 5 seconds. Otherwise the other application will fail if it cannot
      open in 5 seconds. One solution to speed up is to prepare the data before
      you are going to write in a memory block and transfer the complete block in
      one pass.

      An alternative way is record locking.

      --
      Bruker’s portfolio of 3D X-ray microscopes (XRM) offers turnkey solutions for non-destructive 3D imaging for a wide variety of industrial and scientific applications. This includes defect detection in casting, machining and additive manufacturing, inspection of complex electro-mechanical assemblies, pharmaceutical packaging, advanced medical tools, etc.



      Comment

      • L

        #4
        Re: Exclusive Writes to a file.

        Hi Olaf

        How do I program opening a file with retries? Can you please put
        some sample code that describes it?

        How can Record locking be used when opening a file for write? A sample
        code will help.

        Thanks,
        Lalasa.

        Comment

        • Olaf Baeyens

          #5
          Re: Exclusive Writes to a file.

          > How do I program opening a file with retries? Can you please put[color=blue]
          > some sample code that describes it?
          >[/color]
          Right now I do not have the time to give any code but it would look
          something like this;

          bool bOpened=false;
          for (int iTries=0; iTries<10; iTries++) {
          bOpened =OpenFile()
          if (bOpened ) break;
          Sleep(1000); // 1 second
          }
          if (bOpened ) {
          --> Do your stuff
          CloseFile();
          } else {
          --> generate error
          }
          [color=blue]
          > How can Record locking be used when opening a file for write? A sample
          > code will help.
          >[/color]
          I do not have experience with .NET at this moment.

          Basically you open the file and then lock a range of bytes you are going to
          read/write , so if another program wants to access that region, then they
          get an error. But the other program is free to change the bytes outside that
          locked region.
          After the process you unlock that region and close the file.





          --
          Bruker’s portfolio of 3D X-ray microscopes (XRM) offers turnkey solutions for non-destructive 3D imaging for a wide variety of industrial and scientific applications. This includes defect detection in casting, machining and additive manufacturing, inspection of complex electro-mechanical assemblies, pharmaceutical packaging, advanced medical tools, etc.




          Comment

          Working...