Detecting network connection

  • Thread starter Thread starter Thore Berntsen
  • Start date Start date
T

Thore Berntsen

Is there any way I can detect when my Pocket PC gets an network connection.
(A event for example) ?

Thore Berntsen
Norway
 
Thore Berntsen said:
Is there any way I can detect when my Pocket PC gets an network connection.
(A event for example) ?

Connection handling is horribly difficult, in my experience. The
ConnectionManager API in OpenNETCF is helpful, but if you want to make
a connection yourself you'll also want to think about stuff like "Is
the device in the cradle?" and "Do I turn the phone on or not?"

Word of warning: to get events out of ConnectionManager, you need to
initialise it in a UI thread, and keep that UI thread pumping.
 
Hi,

I don't think there is a way of doing this, I havent; hear of any at
least.
What you can do is create a thread that checks at interval if your device
has an IP, if it's so then you are connected to somewhere, then you can rise
an event as needed.

The code below will tell you how to get an IP

string sHostName = Dns.GetHostName();

IPHostEntry ipheThisHost =
System.Net.Dns.GetHostByName(sHostName);
IPAddress ipThisAddr = ipheThisHost.AddressList[0]; //Get the first IP of
the device


Cheers,
 
I'm not sure that either of the schemes suggested is going to get you where
you want to go. It *is* possible to detect, at least in some cases, that a
network card has an established connection. However, I'm unsure as to
whether a dialed PPP-type connection will generate this event or not, or
whether a wired Ethernet connection will reliably indicate when it is
disconnected from the wall (this is up to the driver for the adapter).

The scheme involves code that cannot be written in the managed framework, at
the moment, without P/Invoking to a significant number of things. You have
to use CreateMsgQueue() to create a special message queue, then tell NDISUIO
that it should post a message to this queue when a device connects to or
disconnects from the 'network' (again, whatever that means). When a message
is dropped into this queue, a WaitForXXX() call waiting on the queue handle
will wake up and you can read the message from the queue and check for a
network connection that you can use.

Of course, you'll have to do this in a thread to allow your user interface
to run, so you'll also have to build in some way of stopping that thread
when the application exits, figure out whether there is a real connection to
the network or not when a queue event happens, clean up the queue and the
notification, etc. on close, and a number of other things.

So, having said all that, you might want to think about just having the user
tell you, by clicking a button or selecting a menu item, that the network is
available...

Paul T.
 
I agree with Thore.

I spent some time trying to resolve this but could not get any of a
number of different approaches to work on a consistent basis.

I resorted to an app-configurable user-checkbox...
 
Back
Top