File being used by another process? I don't think so!
-
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
-
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
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.
-
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.
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?
-
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
-
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?
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) -
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?
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.
-
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)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.
-
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.
Thanks. I will make the necessary changes. By the way, in which way can this be causing my problem?
-
Thanks. I will make the necessary changes. By the way, in which way can this be causing my problem?
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.
-
Thanks. I will make the necessary changes. By the way, in which way can this be causing my problem?
-
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