Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. File being used by another process? I don't think so!

File being used by another process? I don't think so!

Scheduled Pinned Locked Moved C#
helpdata-structuresquestiondiscussion
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SimpleData

    Hi, I am trying to code an application which reads some data from a NetworkStream, saves it into a buffer byte array and then appending it to a file. Here is the part I am writing the data from the byte array into the file:

    FileStream fs = new FileStream(last_save_location, FileMode.Append, FileAccess.Write);
    fs.Write(message, 8, bytesRead - 8);
    fs.Close();
    fs.Dispose();

    Everything seems to function fine. I can write the data from NetworkStream to the buffer byte array and then into the file for a while. However, after some time, I get the following error:

    The process cannot access the file 'C:\Users\Main\Desktop\test.bin' because it is being used by another process.

    I am quite sure that no other process is using the file and that I close the handle each and every time. What do you think the problem is? Regards, Can

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    The error message is misleading: when it says "... in use by another process..." it may well be, and often is, the same process, not another one. It would better say "... in use by some process...". Now where within your app is the code you have shown? what threads are involved here? could your code be re-entered, e.g. would there be some Application.DoEvents() calls involved? :)

    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

    S 1 Reply Last reply
    0
    • L Luc Pattyn

      The error message is misleading: when it says "... in use by another process..." it may well be, and often is, the same process, not another one. It would better say "... in use by some process...". Now where within your app is the code you have shown? what threads are involved here? could your code be re-entered, e.g. would there be some Application.DoEvents() calls involved? :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      S Offline
      S Offline
      SimpleData
      wrote on last edited by
      #3

      The code is only called once and it is in a thread. I haven't used the code Application.DoEvents() in any part of the application. I've used Control.CheckForIllegalCrossThreadCalls = false when the form is loaded. Do you think that it has something to do with this?

      D L 2 Replies Last reply
      0
      • S SimpleData

        Hi, I am trying to code an application which reads some data from a NetworkStream, saves it into a buffer byte array and then appending it to a file. Here is the part I am writing the data from the byte array into the file:

        FileStream fs = new FileStream(last_save_location, FileMode.Append, FileAccess.Write);
        fs.Write(message, 8, bytesRead - 8);
        fs.Close();
        fs.Dispose();

        Everything seems to function fine. I can write the data from NetworkStream to the buffer byte array and then into the file for a while. However, after some time, I get the following error:

        The process cannot access the file 'C:\Users\Main\Desktop\test.bin' because it is being used by another process.

        I am quite sure that no other process is using the file and that I close the handle each and every time. What do you think the problem is? Regards, Can

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        You can try this :

                using (FileStream fs = new FileStream(last\_save\_location, FileMode.Append, FileAccess.Write))
                {
                    fs.Write(message, 8, bytesRead - 8);
                    fs.Close();
                }
        

        I know nothing , I know nothing ...

        1 Reply Last reply
        0
        • S SimpleData

          The code is only called once and it is in a thread. I haven't used the code Application.DoEvents() in any part of the application. I've used Control.CheckForIllegalCrossThreadCalls = false when the form is loaded. Do you think that it has something to do with this?

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #5

          SimpleData wrote:

          I've used Control.CheckForIllegalCrossThreadCalls = false when the form is loaded

          Why?

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          S 1 Reply Last reply
          0
          • S SimpleData

            The code is only called once and it is in a thread. I haven't used the code Application.DoEvents() in any part of the application. I've used Control.CheckForIllegalCrossThreadCalls = false when the form is loaded. Do you think that it has something to do with this?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            SimpleData wrote:

            Control.CheckForIllegalCrossThreadCalls = false

            don't. It does not solve a problem, all it does is suppressing the exception that has been added in .NET 2.0 for a reason; and it does not remedy all the bad things that are bound to happen. Please read this[^] and fix your code accordingly. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            S 1 Reply Last reply
            0
            • D DaveyM69

              SimpleData wrote:

              I've used Control.CheckForIllegalCrossThreadCalls = false when the form is loaded

              Why?

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              S Offline
              S Offline
              SimpleData
              wrote on last edited by
              #7

              I am accessing the GUI elements within a thread that I've created and I didn't want to work with delegates because I didn't think it would constitute a problem in a minor project like this.

              1 Reply Last reply
              0
              • L Luc Pattyn

                SimpleData wrote:

                Control.CheckForIllegalCrossThreadCalls = false

                don't. It does not solve a problem, all it does is suppressing the exception that has been added in .NET 2.0 for a reason; and it does not remedy all the bad things that are bound to happen. Please read this[^] and fix your code accordingly. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                S Offline
                S Offline
                SimpleData
                wrote on last edited by
                #8

                Thanks. I will make the necessary changes. By the way, in which way can this be causing my problem?

                L L 2 Replies Last reply
                0
                • S SimpleData

                  Thanks. I will make the necessary changes. By the way, in which way can this be causing my problem?

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #9

                  WinForm Controls aren't thread safe and get accessed quite frequently by the main thread (responding to user actions, repaint messages, etc), so operating them from some other thread as well as from the main thread sooner or later will cause problems. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  1 Reply Last reply
                  0
                  • S SimpleData

                    Thanks. I will make the necessary changes. By the way, in which way can this be causing my problem?

                    L Offline
                    L Offline
                    lukeer
                    wrote on last edited by
                    #10

                    It doesn't cause the problem, but it could very well mask the cause of the problem so you won't see it.

                    Ciao, luker

                    1 Reply Last reply
                    0
                    • S SimpleData

                      Hi, I am trying to code an application which reads some data from a NetworkStream, saves it into a buffer byte array and then appending it to a file. Here is the part I am writing the data from the byte array into the file:

                      FileStream fs = new FileStream(last_save_location, FileMode.Append, FileAccess.Write);
                      fs.Write(message, 8, bytesRead - 8);
                      fs.Close();
                      fs.Dispose();

                      Everything seems to function fine. I can write the data from NetworkStream to the buffer byte array and then into the file for a while. However, after some time, I get the following error:

                      The process cannot access the file 'C:\Users\Main\Desktop\test.bin' because it is being used by another process.

                      I am quite sure that no other process is using the file and that I close the handle each and every time. What do you think the problem is? Regards, Can

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #11

                      Almost always, this is because the log is being called twice, from different threads, in a shorter period than the file takes to open and close. Synchronise the file calls (that is, put a lock block around them).

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups