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# Netzwerk connection prüfen

Empfohlene Antworten

Hallo Leutz,

C# .net 1.1

mal wieder ein Problem.

Ich möchte gern ermitteln ob eine Netzwerkverbindung besteht.

Meine Code verursacht einen Speicherüberlauf da ich es jede 1 Sekunde einmal prüfen möchte.

Vielleicht gibt es aber auch eine Möglichkeit auf ein Event zu warten. Hat da einer eine Idee?

...

ConnectionState Description = 0;

m_bIsNetworkConnected = bool.Parse(InternetGetConnectedState(ref Description, 0).ToString());

...

Das ist doch das selbe was ich auch benutze... Für einmal prüfen kein Thema, aber lass mal die Methode in einer Schleife laufen. Damit bekommst dann auch so einen Effekt :-)

Wie sieht denn deine Schleife aus? Poste mal den Code. Normalerweise sollte es bei nur jeder Sekunde keine Probleme geben:


 static void Main() {

            Thread t = new Thread(new ThreadStart(CheckInetConn));

            t.Start();

            Console.ReadLine();


        }


        public static void CheckInetConn() {

            InternetConnectionState flags = 0;

            while(true) {

                Console.WriteLine(

                    "InternetGetConnectedState : {0} - {1}",

                    (InternetGetConnectedState(ref flags, 0) ? "ONLINE" : "OFFLINE"), flags);

                Thread.Sleep(1000);

            }

        }

So werd ich es nun machen... THX

...

private void NetworkWatcherStart()

{

WqlEventQuery query =

new WqlEventQuery("__InstanceModificationEvent",

new TimeSpan(0,0,5),

"TargetInstance isa \"Win32_NetworkAdapterConfiguration\"");

ManagementEventWatcher eventWatcher = new ManagementEventWatcher(query);

eventWatcher.EventArrived += new EventArrivedEventHandler(Delegate_EventArrived);

eventWatcher.Start();

}

private void Delegate_EventArrived(object sender, EventArrivedEventArgs e)

{

m_bIsNetworkConnected = IsNetworkConnected();

}

private bool IsNetworkConnected()

{

bool connected = SystemInformation.Network;

if (connected)

{

connected = false;

System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT NetConnectionStatus FROM Win32_NetworkAdapter");

foreach (System.Management.ManagementObject networkAdapter in searcher.Get())

{

if (networkAdapter["NetConnectionStatus"] != null)

{

if (Convert.ToInt32(networkAdapter["NetConnectionStatus"]).Equals(2))

{

connected = true;

break;

}

}

}

searcher.Dispose();

}

return connected;

}

...

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Konto

Navigation

Suchen

Suchen

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.