We currently have a service that reads different TAGs from WINCC and inserts those values into a database. It is a process where thousands of tags are read.
Normally the service works well, but sometimes, we do not know the reason, it gets caught reading some TAG (this fact is random and we could not identify the reason) and you have to restart it.
To solve it the solution that has occurred to me is that the reading and writing of these tags are done in a separate thread and that if, after a certain time, that thread does not respond or has not finished the job it can be killed for re-launch it later.
I have tried using IAsyncResult in the following way:
StatisticalData data = enviosCompletados;
IAsyncResult ar = data.BeginInvoke(null, null);
DateTime fin = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second).AddMinutes(1);
while (!ar.IsCompleted && (DateTime.Now < fin))
{
System.Diagnostics.Debug.WriteLine("Posicion " + porcentajecompletado);
}
if (!ar.IsCompleted || !envioCorrecto)
{
Debug.WriteLine("Final " + porcentajecompletado, data.EndInvoke(ar).ToString() + " ...Segundos");
// borrar Tablas
}
else
{
// proceso completado correctamente
}
to simulate that the process has stalled:
while (1==1){}
in Completed shipping.
However, I found that after the
data.EndInvoke(ar)...
the while still alive.
I would like to know how to kill the "data" process or if there is a better alternative, perhaps using tasks or background ... etc.
The key in this service would be to control if a process has finished or not in a certain time and if it has not finished being able to kill it
Thank you very much.