File lock problem

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

    File lock problem

    Occasionally when I try and save a document:

    -----------------------
    xmlDoc.Save(App lication.Startu pPath + @"\Config.xml") ;
    -----------------------

    I get this error saying that the file could not be accessed because its
    locked by another process or something (I cant get it to do it now - because
    I want it to crash...)
    Is there a way to check whether a file is locked by another process befre
    trying to save it? If it is locked is there a way to terminate the other
    process or somewhow free it up so that it can be saved?

    Thanks,
    Grant


  • CheshireCat

    #2
    Re: File lock problem

    I can't answer your specific question but if I try to open a file I use
    try/catch and usually loop the process over a short time period with some
    thread.sleep(0) thrown in.


    Comment

    • CheshireCat

      #3
      Re: File lock problem

      I can't answer your specific question but if I try to open a file I use
      try/catch and usually loop the process over a short time period with some
      thread.sleep(0) thrown in.


      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: File lock problem

        Hi,

        No, AFAIK there is no way to know if a file is locked beforehand, you have
        to open it and it will give you error, use a try/catch block .

        It's possible to know what process is using a given file, filemon from
        sysinternals.co m does it. how ? frankly I have no idea , IIRC there is no a
        easy way to get this info. of course there is not a managed way of doing it.


        cheers,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation



        "Grant" <gpsnett@hotmai l.com> wrote in message
        news:uFozAFcRFH A.1268@TK2MSFTN GP14.phx.gbl...[color=blue]
        > Occasionally when I try and save a document:
        >
        > -----------------------
        > xmlDoc.Save(App lication.Startu pPath + @"\Config.xml") ;
        > -----------------------
        >
        > I get this error saying that the file could not be accessed because its
        > locked by another process or something (I cant get it to do it now -
        > because I want it to crash...)
        > Is there a way to check whether a file is locked by another process befre
        > trying to save it? If it is locked is there a way to terminate the other
        > process or somewhow free it up so that it can be saved?
        >
        > Thanks,
        > Grant
        >[/color]


        Comment

        • Ignacio Machin \( .NET/ C# MVP \)

          #5
          Re: File lock problem

          Hi,

          No, AFAIK there is no way to know if a file is locked beforehand, you have
          to open it and it will give you error, use a try/catch block .

          It's possible to know what process is using a given file, filemon from
          sysinternals.co m does it. how ? frankly I have no idea , IIRC there is no a
          easy way to get this info. of course there is not a managed way of doing it.


          cheers,

          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation



          "Grant" <gpsnett@hotmai l.com> wrote in message
          news:uFozAFcRFH A.1268@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Occasionally when I try and save a document:
          >
          > -----------------------
          > xmlDoc.Save(App lication.Startu pPath + @"\Config.xml") ;
          > -----------------------
          >
          > I get this error saying that the file could not be accessed because its
          > locked by another process or something (I cant get it to do it now -
          > because I want it to crash...)
          > Is there a way to check whether a file is locked by another process befre
          > trying to save it? If it is locked is there a way to terminate the other
          > process or somewhow free it up so that it can be saved?
          >
          > Thanks,
          > Grant
          >[/color]


          Comment

          • Grant

            #6
            Re: File lock problem

            I can open the file ok and make modifications - its when I do the save bit
            that it fails - is this where I would expect it to fail or when Im opening
            the file?

            Either way though Ill try open the file in a try catch block on a seperate
            thread (now to learn how to do this on a seperate thread...)

            Thanks for your help.

            Grant

            "CheshireCa t" <hah@notonyourn elly.coom> wrote in message
            news:%237piHlcR FHA.3944@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            >I can't answer your specific question but if I try to open a file I use
            >try/catch and usually loop the process over a short time period with some
            >thread.sleep(0 ) thrown in.
            >
            >[/color]


            Comment

            • Grant

              #7
              Re: File lock problem

              I can open the file ok and make modifications - its when I do the save bit
              that it fails - is this where I would expect it to fail or when Im opening
              the file?

              Either way though Ill try open the file in a try catch block on a seperate
              thread (now to learn how to do this on a seperate thread...)

              Thanks for your help.

              Grant

              "CheshireCa t" <hah@notonyourn elly.coom> wrote in message
              news:%237piHlcR FHA.3944@TK2MSF TNGP10.phx.gbl. ..[color=blue]
              >I can't answer your specific question but if I try to open a file I use
              >try/catch and usually loop the process over a short time period with some
              >thread.sleep(0 ) thrown in.
              >
              >[/color]


              Comment

              • Willy Denoyette [MVP]

                #8
                Re: File lock problem


                "Grant" <gpsnett@hotmai l.com> wrote in message
                news:OQOrlgdRFH A.204@TK2MSFTNG P15.phx.gbl...[color=blue]
                >I can open the file ok and make modifications - its when I do the save bit
                >that it fails - is this where I would expect it to fail or when Im opening
                >the file?
                >
                > Either way though Ill try open the file in a try catch block on a seperate
                > thread (now to learn how to do this on a seperate thread...)
                >
                > Thanks for your help.
                >
                > Grant[/color]

                That means you still have your file open on the other thread, right?
                If that's true, are you sure the file is opened for shared RW access?

                Willy.


                Comment

                • Willy Denoyette [MVP]

                  #9
                  Re: File lock problem


                  "Grant" <gpsnett@hotmai l.com> wrote in message
                  news:OQOrlgdRFH A.204@TK2MSFTNG P15.phx.gbl...[color=blue]
                  >I can open the file ok and make modifications - its when I do the save bit
                  >that it fails - is this where I would expect it to fail or when Im opening
                  >the file?
                  >
                  > Either way though Ill try open the file in a try catch block on a seperate
                  > thread (now to learn how to do this on a seperate thread...)
                  >
                  > Thanks for your help.
                  >
                  > Grant[/color]

                  That means you still have your file open on the other thread, right?
                  If that's true, are you sure the file is opened for shared RW access?

                  Willy.


                  Comment

                  • Cor Ligthert

                    #10
                    Re: File lock problem

                    Grant,

                    What you ask has never been possible with any (regular) Microsoft OS.

                    It would be very nice when we could.

                    They have build themselves a special feature for that in Office.

                    Cor


                    Comment

                    • Grant

                      #11
                      Re: File lock problem

                      how do you get a return value from a seperate thread? Ive got this far but I
                      dont know how to check whether the update was a success or not..

                      Thread updaterThread = new Thread(new ThreadStart(upd ateSomething));
                      updaterThread.S tart();

                      Thanks,
                      Grant

                      "CheshireCa t" <hah@notonyourn elly.coom> wrote in message
                      news:%237piHlcR FHA.3944@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                      >I can't answer your specific question but if I try to open a file I use
                      >try/catch and usually loop the process over a short time period with some
                      >thread.sleep(0 ) thrown in.
                      >
                      >[/color]


                      Comment

                      Working...