Here the entire code:
Main ------>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalledClassWrite;
namespace CalledClassWrite
{
class Program
{
static void Main()
{
int count = 0;
CalledClassWrite.secondClass writeOnLabel = new secondClass();
while (count < 10000)
{
writeOnLabel.writeWhiteInk();
System.Threading.Thread.Sleep(2000);
writeOnLabel.writeBlackInk();
count++;
}
}
}
}
Class invoked:
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
namespace CalledClassWrite
{
class secondClass
{
DateTime utc = DateTime.Now;
public void writeWhiteInk()
{
//Create a new file to write
using (StreamWriter writer = new StreamWriter("C:\\Users\\alberto\\Desktop\\a.txt", true ))
{
writer.WriteLine("White");
writer.WriteLine("{0}",utc);
//Il valore della batteria sarà di input
writer.WriteLine("% Battery" + Environment.NewLine );
}
}
public void writeBlackInk()
{
//Create a new file to write
TextWriter nf = new StreamWriter(@"C:\\Users\\alberto\\Desktop\\a.txt", true);
nf.WriteLine("Black");
nf.WriteLine("{0}",utc);
nf.WriteLine("% Battery\r" + Environment.NewLine);
nf.Close();
}
The output is :
White
28/05/2018 13:51:11
% Battery
Black
28/05/2018 13:51:11
% Battery
White
28/05/2018 13:51:11
% Battery
Black
28/05/2018 13:51:11
% Battery...
For some reason the thread block the seconds...the time is always 13:51:11