2017-06-02 3 views
0

Je veux vérifier si la connexion est toujours ouverte. Si ce n'est pas le cas, j'arrêterai le processus.Comment puis-je vérifier que la connexion HttpListenerRequest est toujours ouverte?

  //Process request code goes here 

     while (!isComplete) { 
      //Check HttpListenerRequest if connection is still open... 

      if (IsTimedOut && !timeoutTriggered) 
      { 
       timeoutTriggered = triggerTimeout(); 
      } 

      Thread.Sleep(100); 
     } 
     stopWatch.Stop(); 
     //Process response code goes here 

Comment puis-je faire cela?

+0

La question n'est pas claire du tout.S'il vous plaît créer https://stackoverflow.com/help/mcve –

Répondre

0

J'ai résolu le problème avec une paire écriture/vidage. Flush déclenche une exception lorsque la connexion est fermée.

try 
{ 
    //Checks whether the connection is still open 
    var encoding = Encoding.Default; 
    byte[] utf8Bytes = Encoding.Convert(encoding, Encoding.UTF8, encoding.GetBytes(" ")); 
    listenerContext.Response.OutputStream.Write(utf8Bytes, 0, utf8Bytes.Length); 
    listenerContext.Response.OutputStream.Flush(); 
} 
catch (Exception e) 
{ 
    Console.WriteLine(e); 
    eventStream.TriggerEvent(new ActionEventArgs(this, ActionEventStreamer.SYSTEM_CANCEL)); 
    break; 
}