I have some C# code that uses a timer to run a method which saves a list of strings to a binary file.
When I compile the code with csc.exe and run the exe file, I get a maximum of 3 runs before the program freezes.
When I run the same code in VS 2019 and build the project, the code runs perfectly fine - it keeps printing until I terminate the program.
Any ideas what the reason for this might be? I have looked for answers and reasons but I cannot find anything.
The csc command used in cmd was:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe test2.cs
and the settings in Visual Studio 2019 was the standard for .NET Console application. I did not change any setting after installation
Here is the code:
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Timers;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Windows.Forms;
namespace ConsoleApp1
{
public class dataFixer
{
public String getTimeStamp(DateTime value)
{
return value.ToString("yyyy-MM-dd-HH:mm:ss");
}
public int lengd { get; set; }
static String filepath;
public ArrayList data
{
get; set;
}
public List<string> Lines { get; set; }
public void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false)
{
using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, objectToWrite);
}
}
public List<List<String>> storedValues { get; set; }
public dataFixer()
{
filepath = "C:\\Users\\APR\\LoopList.txt";
Lines = File.ReadAllLines(filepath).ToList();
storedValues = new List<List<String>>();
}
}
class Program
{
public static void HemtPrint(dataFixer n)
{
var temp = n.Lines.Count;
string tider = n.getTimeStamp(DateTime.Now);
List<String> sublist = new List<String>();
for (int j =0; j < n.Lines.Count; j++)
{
for (int i = 0; i < 20; i++)
{
sublist.Add(n.Lines[j]);
}
n.storedValues.Add(new List<String>(sublist));
sublist.Clear();
}
}
static void Main(string[] args)
{
dataFixer n = new dataFixer();
Console.WriteLine("Hello World!");
var startTimeSpan = TimeSpan.FromSeconds(0);
var periodTimeSpanGet= TimeSpan.FromSeconds(10);
var timer = new System.Threading.Timer((e) =>
{
Console.WriteLine("*** Running Code *** ");
Console.WriteLine(DateTime.Now);
HemtPrint(n);
n.WriteToBinaryFile<List<List<String>>>("C:\\Users\\APR\\savedData.bin",n.storedValues);
Console.WriteLine("*** Finished code *** ");
Console.WriteLine(DateTime.Now);
}
, null, startTimeSpan, periodTimeSpanGet);
Console.ReadKey();
}
}
}
EDIT:
I have now tried with the latest mondo and the standard settings ant it gives me the same results as the latest visual studio.