0

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.

  • 4
    Can we see some code? – Jonathon Chase Feb 25 '20 at 21:58
  • 1
    We need to see the code, and the compiler versions, before this can be answered. –  Feb 25 '20 at 22:00
  • 1
    Please read [ask] and provide a [mcve]. – Ondrej Tucny Feb 25 '20 at 22:00
  • 1
    In addition to a [mcve] and compiler versions, we'll need the compiler settings in VS and the `csc` command line. – Flydog57 Feb 25 '20 at 22:02
  • 2
    Please clean up the code and remove all the unnecessary whitespace and format it with proper indentation, etc. – Chris Dunaway Feb 25 '20 at 22:18
  • I have added and cleaned the code as requested. – Testertester Feb 26 '20 at 04:55
  • 1
    It works in VS because you'll build the Debug configuration and have a debugger attached. Which is enough to prevent the timer from getting garbage-collected. Add `GC.KeepAlive(timer);` at the end of Main to fix the bug. Backgrounder on this behavior [is here](https://stackoverflow.com/a/17131389/17034) and [here](https://stackoverflow.com/questions/18136735/can-timers-get-automatically-garbage-collected). – Hans Passant Feb 27 '20 at 22:42
  • This latest comment actually solved the issue. Big thanks for the help. Sometimes you dont see the bigger picture and have difficulties formulating the correct question. – Testertester Feb 28 '20 at 14:48

0 Answers0