Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

C# StreamReader

Empfohlene Antworten

Veröffentlicht

Hallo,

ich habe ein Problem mit StreamReader. Ich würde gerne alle 500 ms überprüfen, ob auf einem Stream eine neue Nachricht vorhanden ist.

Dafür habe ich folgenden code:

Connect:


this.client = new TcpClient(_ip, _port);

this.stream = this.client.GetStream();

this.streamReader = new System.IO.StreamReader(this.stream);

this.streamWriter = new System.IO.StreamWriter(this.stream);

auslesen tu ich das nun mit
streamReader.Read(buffer, index, count);

Das Problem ist dabei aber, das das Programm blockiert, wenn kein Input vorhanden ist.

Bei php gibts die einfache möglichkeit, den stream auf nonblocking zu setzen, für c# habe ich noch nichts dergleichen gefallen.

Könnt ihr mir da helfen? Wie kann ich es machen, das das Read nicht mehr das Programm blockiert, sondern einfach wenn nichts da ist nichts macht.

MfG

Funfare

Schreib dir doch einfach mal eine kleine Methode oder Funktion die dir das prüft.

Bsp.:


 bool DataAvailable(ref StreamReader rdReader,int intInterval,int Timeout)

{

DateTime t1 = DateTime.Now;

DateTime t2;

while((!rdReader.BaseStream.CanRead) && (tcpClient.Available<=0))

{

Thread.Sleep(intInterval);

t2 = DateTime.Now;

if(t2.Subtract(t1).Milliseconds > Timeout){

return false;

}

}

return true;

}

Ich entschuldige mich für etwaige Syntaxfehler. Ist freihand verfasst und sollte nur als kleine Richtline gelten.

lg

Gateway

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.