There is an array with timers work intervals. Each timer must record these intervals in a text file.

static void Main(string[] args) { int[] timerDelays = { 10000, 15000, 10000 }; foreach (int d in timerDelays) { var timer = new System.Timers.Timer(d); timer.Elapsed += OnTimeout; timer.AutoReset = true; timer.Enabled = true; } Console.Read(); } private static void OnTimeout(Object source, ElapsedEventArgs e) { int[] timerDelays = { 10000, 15000, 10000 }; StreamWriter strwrt = new StreamWriter("C:\\ΠΏΡ€ΠΈΠΌΠ΅Ρ€.txt", true); strwrt.WriteLine(timerDelays); Console.WriteLine("НаТмитС ENTER Ρ‡Ρ‚ΠΎ Π±Ρ‹ Π²Ρ‹ΠΉΡ‚ΠΈ ΠΈΠ· ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹"); Console.Read(); } 

Here is my code. Tell me or give another example, please. I have an error when writing to the file.

  • Add the terms of your task. What you need to do and where is the mistake - MaximK
  • There is an array of timer, which timer runs every 1 sec, 1.5 sec and 1 sec. And writes these numbers into a text file - propro17
  • @ MaksimKutovoy File.WriteAllLines (path, timerDelays); // there is an error - propro17
  • Need to solve one problem - Simultaneous access to the file from different timers. You have 3 timers trying to write to the same file. Dig here - Asynchronous file output - MaximK
  • @ MaksimKutova at least show, using my example, writing an array to a file, please !!! - propro17

2 answers 2

 class Program { static void Main(string[] args) { // миллисСкунда - это 1/1000 сСкунды int[] timerDelays = { 1000, 1500, 1000 }; foreach (int d in timerDelays) { var timer = new System.Timers.Timer(d); timer.Elapsed += OnTimeout; timer.AutoReset = true; timer.Enabled = true; } // ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ Π² Main, Π° Π½Π΅ Π² ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ΅ Ρ‚Π°ΠΉΠΌΠ΅Ρ€Π° - Ρ‚Π°ΠΉΠΌΠ΅Ρ€Ρ‹ Π±ΡƒΠ΄ΡƒΡ‚ Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π² Ρ„ΠΎΠ½Π΅ Console.WriteLine("НаТмитС ENTER Ρ‡Ρ‚ΠΎ Π±Ρ‹ Π²Ρ‹ΠΉΡ‚ΠΈ ΠΈΠ· ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹"); Console.Read(); } // ΠΎΠ±Ρ‰ΠΈΠΉ для всСх ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ²/Ρ‚Π°ΠΉΠΌΠ΅Ρ€ΠΎΠ² ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ private static object fileLock = new object(); private static void OnTimeout(Object source, ElapsedEventArgs e) { int[] timerDelays = { 10000, 15000, 10000 }; // Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠ° Π½Π° врСмя записи lock (fileLock) { // запись массива ΠΊΠ°ΠΊ Ρ‚Ρ€Π΅Ρ… строк, Π² Π½ΠΎΠ²Ρ‹ΠΉ Ρ„Π°ΠΉΠ» ΠΈΠ»ΠΈ Π² ΠΊΠΎΠ½Π΅Ρ† ΡΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‰Π΅Π³ΠΎ File.AppendAllLines(@"C:\temp\ΠΏΡ€ΠΈΠΌΠ΅Ρ€.txt", timerDelays.Select(t => t.ToString())); } } } 

    There are two difficulties in the task:

    1. Write array to file

    MSDN: File.WriteAllLines

    File.WriteAllLines - Creates a new file, writes the specified array of strings to this file using the specified encoding, then closes the file.

    Just in case, reading from a file: MSDN: File.ReadAllLines

    File.ReadAllLines - Opens a text file, reads all lines of the file into an array of strings, and then closes the file.

     static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { int[] timerDelays = { 10000, 15000, 10000 }; string[] str = timerDelays.Select(s => s.ToString()).ToArray(); string path = @"c:\temp\ΠΏΡ€ΠΈΠΌΠ΅Ρ€.txt"; if (!File.Exists(path)) File.WriteAllLines(path, str); else File.AppendAllLines(path, str); } 

    2. Simultaneous access to a file from different timers

    one of the solutions. The FileStream has the 4th flag in the FileShare.None constructor, which rejects the use of the file if it is busy. Scroll it in while (true) until access appears.

      static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { int[] timerDelays = { 10000, 15000, 10000 }; FileStream fs = null; while (true) { try { fs = new FileStream(@"C:\temp\ΠΏΡ€ΠΈΠΌΠ΅Ρ€.txt", FileMode.Append, FileAccess.Write, FileShare.None); break; } catch { System.Threading.Thread.Sleep(10); } } using (StreamWriter sr = new StreamWriter(fs)) { foreach (int t in timerDelays) { sr.WriteLine(t); Console.WriteLine(t); } sr.Close(); sr.Dispose(); } fs.Close(); fs.Dispose(); } 
    • You can show in my example. I just get an error - propro17
    • I know that there are two difficulties, and I ask you to show an example of a code to write to a file, and with the timer I will try it myself. I beg you to) - propro17
    • can you give an example? Not? - propro17
    • 2
      @ propro17: Well, open the same documentation, everything is painted there. - VladD