I have a thread that controls another thread, the problem is that I need the other thread to restart every x time and when I try to start it again I can not make it start.
System.Threading.ThreadStateException: 'Thread running or terminated; can not be restarted.'
Is there any way to reuse / restart the thread?
static void Main(string[] args)
{
ThreadStart consola = new ThreadStart(controladorHilos);
Thread thread = new Thread(consola);
thread.Start();
}
Here is where the thread is created , where I get the error. It happens to me so much putting the thread as proceso
as putting it with subproceso
.
The thing is that later I'll need the thread to control more things.
public static void controladorHilos()
{
ThreadStart consola = new ThreadStart(hilo);
Thread hconsola = new Thread(consola);
log("se lanza el hilo");
hconsola.Start()
}
According to the life cycle of the threads, once the thread ends, there is no way to lift it, is there any alternative to this? Any way for the thread to abort, stop, end, and then get up? It's no good that the thread is paused, it has to start all of 0
The thread method: The thread method opens a socket to an ip address, via telnet, launches several commands and begins to receive information from the socket. What happens is that the session opened on the socket expires every X time or loses connection every X time, the socket does not send me any message that the connection has been lost so that thread continues listening to the socket, even if there is no connection. The socket opens to another headquarters, so I can not think of any better way to solve it.
public static void hilo(){
byte[] bytes = new Byte[256];
String data = null;
TcpClient cliente = new TcpClient();
while (true)
{
NetworkStream stream = cliente.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Recibido: {0}", data);
data = data.ToUpper();
byte[] msg;
if (data.Contains("login")){
console.write("login username password");
//Se abre buffer de datos y empiezo a recibir datos
}
if (data.Contains("Cualquier mensaje recibido del socket"))
{
msg = System.Text.Encoding.ASCII.GetBytes("queueinfo" + Environment.NewLine);
stream.Write(msg, 0, msg.Length);
//Aqui hay inserciones a una BBDD etc. Todo funciona ok.
}
}
}
}
I have to clarify that in the thread method no error occurs, the error comes from the socket to which this thread points, when the connection is lost with it, it always passes every X time, so, I need to restart all this and I do not know exactly how I could do it.