Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>Use <code>using</code> around all your objects that are <code>IDisposable</code>. <code>using</code> will ALWAYS call the method <code>Dispose</code>, even if there is an exception.</li> <li>You did close your reader, but did not close the filestream.</li> <li><p>This code can be made much shorter, see my second example at the bottom of my answer.</p> <pre><code>private static void write_history(int index, int time_in_sec, int[] sent_resources) { string filepath = "Config\\xxx.txt"; int writing_index = 0; if (File.Exists(filepath)) { System.Threading.Thread.Sleep(5000); using(FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read) using(StreamReader reader = new StreamReader(stream)) { string temp = reader.ReadToEnd(); } for (int i = 0; i &lt; 20; i++) { if (temp.IndexOf("&lt;hst_" + i.ToString() + "&gt;") == -1) { writing_index = i; break; } } } System.Threading.Thread.Sleep(5000); // write to the file using(StreamWriter writer = new StreamWriter(filepath, true)) { writer.WriteLine("&lt;hst_" + writing_index.ToString() + "&gt;" + DateTime.Now.AddSeconds(time_in_sec).ToString() + "|" + sent_resources[0] + "|" + sent_resources[1] + "|" + sent_resources[2] + "|" + sent_resources[3] + "&lt;/hst_" + writing_index.ToString() + "&gt;"); } } </code></pre></li> </ol> <p>Shorter version:</p> <pre><code> private static void write_history(int index, int time_in_sec, int[] sent_resources) { string filepath = "Config\\xxx.txt"; int writing_index = 0; if (File.Exists(filepath)) { System.Threading.Thread.Sleep(5000); string temp = File.ReadAllText(filepath); for (int i = 0; i &lt; 20; i++) { if (temp.IndexOf("&lt;hst_" + i.ToString() + "&gt;") == -1) { writing_index = i; break; } } } System.Threading.Thread.Sleep(5000); // write to the file File.WriteAllText(filepath, "&lt;hst_" + writing_index.ToString() + "&gt;" + DateTime.Now.AddSeconds(time_in_sec).ToString() + "|" + sent_resources[0] + "|" + sent_resources[1] + "|" + sent_resources[2] + "|" + sent_resources[3] + "&lt;/hst_" + writing_index.ToString() + "&gt;"); } </code></pre> <p>In case of multi threading:</p> <pre><code>private static readonly object _syncLock = new object(); private static void write_history(int index, int time_in_sec, int[] sent_resources) { lock(_syncLock) { string filepath = "Config\\xxx.txt"; int writing_index = 0; if (File.Exists(filepath)) { System.Threading.Thread.Sleep(5000); string temp = File.ReadAllText(filepath); for (int i = 0; i &lt; 20; i++) { if (temp.IndexOf("&lt;hst_" + i.ToString() + "&gt;") == -1) { writing_index = i; break; } } } System.Threading.Thread.Sleep(5000); // write to the file File.WriteAllText(filepath, "&lt;hst_" + writing_index.ToString() + "&gt;" + DateTime.Now.AddSeconds(time_in_sec).ToString() + "|" + sent_resources[0] + "|" + sent_resources[1] + "|" + sent_resources[2] + "|" + sent_resources[3] + "&lt;/hst_" + writing_index.ToString() + "&gt;"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload