Wouldn't using the "lock" directive be enough?
SimpleData
Posts
-
A problem while transferring data over the NetworkStream -
A problem while transferring data over the NetworkStreamCan you elaborate on "attempting to be clever by using threads for reading and writing"? Because it sounds like something I might have done. :)
-
A problem while transferring data over the NetworkStreamI can't say that it is deterministic since if I pause the program at certain intervals, it can complete the transmission. If I don't pause the program at all, it stops after transferring 200-300KB. I am aware that what I am doing is just slowing down the transfer rate, but I am new to this are of programming and currently trying to develop this project to increase my experience. I will now add a timeout system to the server and request the rest of the data if nothing is incoming.
-
A problem while transferring data over the NetworkStreamThe connection is still intact, it doesn't drop. The only problem is that the server stops requesting chunks.
-
A problem while transferring data over the NetworkStreamI am aware that the information with which I've provided you is less than sufficient, but it is really hard for me to isolate the code that works on the transfer, and there is a chance that isolating it from the rest of the application will leave you with no problem to solve. The problem may as well be stemming from the structure of the rest of the application. I will check if there is a problem with the file size tomorrow, but I don't think that is the problem. When I occasionally place a breakpoint and break the program, it manages to transfer a little bit more data (around 300KB more).
-
A problem while transferring data over the NetworkStreamThank you. Why haven't I thought of that. :) I will add that mechanism first thing tomorrow. By the way, even though this will probably solve my problem; there must still be a problem in my system since it is really unlikely for a package to be lost on a local TCP connection.
-
A problem while transferring data over the NetworkStreamYeah, sorry about that. While I am trying to transfer the data, it is stuck at some point. The server doesn't make any other requests and hence, the client doesn't send any more data. I have no idea why nothing new happens.
-
A problem while transferring data over the NetworkStreamHi, I am trying to send a big file over a TCP connection using NetworkStream and TCPClient. Both the client and the server is written in C#. There is no problem while transferring small files around the size of 300KB. My buffer size is 2KB and here is how the protocol works. -Server requests the file. -Client tells the server the size of the file, and then sends the first 2KB of it. -The server asks for another chunk of the file -like, send 2KB data, starting from 42649- -Client sends the chunk -Server asks for more, and client sends it... -When the end of file is reached, the client acknowledges the server that transfer is complete. Without looking at the code, since it is too long to be posted here, can you see any possible problems that may be causing this problem? I even tried sleeping the thread while receiving and processing the incoming chunks of data. Regards
-
File being used by another process? I don't think so!Thanks. I will make the necessary changes. By the way, in which way can this be causing my problem?
-
File being used by another process? I don't think so!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.
-
File being used by another process? I don't think so!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?
-
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
-
Speed difference between two functions with unknown reasonThank you all for your help. Now I got the idea and solved the problem. It would take me much longer to spot the cause by myself.
-
Speed difference between two functions with unknown reasonNow I got the idea. Thank you for your answers, this was a really hard thing to spot, for me. :)
-
Speed difference between two functions with unknown reasonHi, I am not sure I understood your point exactly. if(pos % 204800 == 0) block is used in both cases but they still have speed differences.
-
Speed difference between two functions with unknown reasonI am using the Process Explorer from SysInternals and the first method creates an IO traffic with the magnitude of approx. 32MB, but the second one can't even exceed 1MB.
-
Speed difference between two functions with unknown reasonHi, I have two methods, which are esentially doing similar jobs but have drastic speed differences. I don't know what is causing this speed difference. Here are the methods. The first one is like 100 times faster.
private void SaveNativeBinary(string file, long origin, long length, string opt)
{
SetSSVisible(StatusStrip, pbSmallPerc, true);FileStream fs = null; FileStream writer = null; try { fs = new FileStream(file, FileMode.Open, FileAccess.Read); } catch { throw new Exception("ERR"); } try { writer = new FileStream(opt, FileMode.Create, FileAccess.ReadWrite); } catch { throw new Exception("ERR"); } fs.Seek(origin, SeekOrigin.Begin); for (long pos = 0; pos < length; pos++) { if (pos % 204800 == 0) // 200KB'da bir güncelle { SetSSValue(StatusStrip, pbSmallPerc, Convert.ToInt32(PercentageCalc(pos, length)) ); SetSSText(StatusStrip, lblStatus, "Aktarılıyor: " + BtoKB(pos).ToString() + " / " + BtoKB(length).ToString() + " KB (%" + PercentageCalc(pos, length) + ")"); } writer.WriteByte((byte)fs.ReadByte()); } fs.Close(); fs.Dispose(); writer.Close(); writer.Dispose(); SetSSVisible(StatusStrip, pbSmallPerc, false); }
private void AppendBinToBin(string Container, string toAppend)
{
SetSSVisible(StatusStrip, pbSmallPerc, true);
FileStream fsA, fsB = null;
long tempLength = GetFileLength(toAppend);try { fsA = new FileStream(Container, FileMode.Open, FileAccess.ReadWrite); } catch { throw new Exception("ERR"); } try { fsB = new FileStream(toAppend, FileMode.Open, FileAccess.Read); } catch { throw new Exception("RRR"); } try { fsA.Seek(0, SeekOrigin.End); for (long pos = 0; pos < fsB.Length; pos++) { if (pos % 204800 == 0) // 200KB'da bir güncelle { SetSSValue( StatusStrip, pbSmallPerc, Convert.ToInt32(PercentageCalc(pos, tempLength)) ); SetSSText( StatusStrip, lblStatus, "Aktarılıyor: " + BtoKB(pos).ToString() + " / " + BtoKB(tempLength).ToString() + " KB (%" + PercentageCalc(pos, tempLength) + ")" );
-
How to search for a specific string in a file?Thanks for the advices. I will change the code accordingly. This algorithm covers my needs. It works, it is fast and it doesn't consume much RAM. :)
-
How to search for a specific string in a file?I am open to suggestions.
-
How to search for a specific string in a file?Yes, but I think that is not a problem for me. I am looking for a string in the file, no matter where it is. I think code can express everything, in a better way. Here is my code:
private long DigBinary(string file, string strToDig)
{
FileStream fs = null;char\[\] chAim = strToDig.ToCharArray(); char chTemp = '0'; long latestHitBeginningLocation = 0; int locationInArray = 0; try { fs = new FileStream(file, FileMode.Open, FileAccess.Read); } catch { throw new Exception("An error occured while creating the stream."); } try { while (locationInArray < chAim.Length) { chTemp = (char)fs.ReadByte(); if( chTemp != chAim\[locationInArray\] ) locationInArray = 0; if (chTemp == chAim\[locationInArray\]) { if (locationInArray == 0) latestHitBeginningLocation = fs.Position - 1; if (locationInArray == chAim.Length) break; locationInArray++; } else { locationInArray = 0; latestHitBeginningLocation = 0; } } } catch { throw new Exception("An error occured while reading the file."); } finally { if (fs != null) { fs.Close(); fs.Dispose(); } } return latestHitBeginningLocation; }
And yes, I know that my try-catch is useless. :D