2
This code is the fragment of a method that reads a text file and sends its content line by line, where each sent line receives a confirmation of receipt. I would like to delete from the file the sent line so get your confirmation of receipt, so that, if the transmission is interrupted in the middle of the file, I do not send the lines that have already been sent, thus avoiding duplicating the information received by the equipment. Someone could help me with this ?
//Path ex : "/home/dhnqe/DataContainer.txt"
if (File.Exists(path))
{
StreamReader file = new StreamReader(path);
int numeroLinhas = File.ReadAllLines(path).Length;
while ((linha = file.ReadLine()) != null)
{
linha = string.Format(linha + "\r\n");
for (int i = 0; i< 3; i++)
{
controle = false;
SendLineByRadio(address, line); //Enviar Linha
int start = Environment.TickCount;
do
{
...
...
} while (!controle && !timeout);
if (controle)
{
if (Confirmation == "ReceiveOK")//Caso receba a confirmação
{
Console.WriteLine("Data Sent = OK");
clearData();
controle = false;
//INSERIR FUNCAO PARA APAGAR LINHA ENVIADA DO ARQUIVO TXT
i = 10;
}
}
if (timeout)
{
Console.WriteLine("Data Sent = Confirmation Timeout");//Caso não recebe a confirmacao
clearData();
controle = true;
if (i == 2)
{
Console.WriteLine("Error: Response not received");
clearData();
Thread.CurrentThread.Abort();
}
}
}
}
Do you need to delete the lines? This is not very efficient. Is the confirmation synchronous? i.e. you only send a file line when you confirm the previous one? If so, why not just save the number of the last line sent in another temporary file ex:Datacontainer.txt.tmp ? In the end, you delete both. So you recover the state if you interrupt the execution of the application.
– nuno
It is that when a line is sent the program waits for a message from the recipient confirming its receipt, home does not receive confirmation the transmission is aborted.
– Elliot Alderson
But, for example, if there was a file with 10 lines and the transmission was aborted in the fifth line the recipient had already received the previous four, and in that case if I send them again another transmission request, it would contain duplicate information, since the destination stores all transferred information.
– Elliot Alderson
As can also be seen in the code, this method is executed by a new Thread whenever there is a communication request, which is aborted if it does not receive confirmation of receipt of the line.
– Elliot Alderson
The idea of temporary file can work, I will try to implement here and test, I will return on this
– Elliot Alderson
Okay, changing a file you’re iterating on is not a very good idea. Don’t you have control over the server you send the information to? If a line was received and confirmation didn’t arrive, how do you know? in this case will forward the information, the ideal was to have a protocol to avoid these situations.
– nuno
Yes there is a protocol where, I send the line and I wait for a Confirmation Message with a timeout, if you exceed this timeout the line is resubmitted, this happens 3 times, if you do not receive the reply the process is finished and the device needs to make another transmission request. This transmission is done by Radio Frequency, the destination is a microcontrolled equipment, so the only way to know if the received line is with the same message.
– Elliot Alderson
I also thought of reading the file and creating a list with the lines and while each line is sent I delete its entry, in case the transmission has to be aborted, I rewrite the file with the remaining lines
– Elliot Alderson
http://stackoverflow.com/questions/668907/how-to-delete-a-line-from-a-text-file-in-c
– Marco Souza
thanks guys, the temporary file worked, follow the answer to help someone in the future
– Elliot Alderson