NetworkChange.NetworkAddressChanged event stops working - c#

I am using this simple way to detect network connect/disconnect events:
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
...
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
RaiseNewtorkChange();
}
static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
RaiseNewtorkChange();
}
static void RaiseNewtorkChange()
{
...
}
The problem is that sometimes NetworkAddressChanged event just stops working, after it was fired several times. Does anyone have an idea why this can happen?
Alternatively, is there another way to handle network connect/disconnect events, using C# or C/C++. Maybe there is such functionality in Windows API or WMI? I need notification on LAN/WiFi network connecting/disconnecting, without polling.

It seems to me that you need to use the Native Wifi API.
Take a look at the function WlanRegisterNotification, in particular this notification:
WLAN_NOTIFICATION_SOURCE_ACM: Registers for notifications generated by the auto configuration module.
Windows XP with SP3 and
Wireless LAN API for Windows XP with SP2: Only the
wlan_notification_acm_connection_complete and
wlan_notification_acm_disconnected notifications are available.
Disclaimer: I have used Native Wifi API in the past, but I have never used this particular feature.

Related

Bluetooth LE event handler quit without exception windows phone 8.1

I am doing a Bluetooth LE app on wp8.1 runtime app.
I have registered a value change event to listen when new data are coming.
heartRateMeasurementCharacteristic.ValueChanged += heartRateMeasurement_DataChanged;
private void heartRateMeasurement_DataChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
//do things
}
after a while the event is not triggered anymore, and rpgram throw NO exception(I checked runtime, memory etc). How can I debug this? Is that possible the event thread is down leaves without any exception?
under condition that BLE device is keep sending signal.
It's an old post, but the answer is to set heartRateMeasurementCharacteristic as a global variable.

Handle the event when device connect to cradle windows ce opennetcf in C#

I develop an application on windowCE 5.0 with opennetcf library.
I want to check WHEN my Device is connected to Cradle. It means I want to handle the event of plugged the device to cradle or other similar.
My purpose is that when Device is connected to Cradle, I disable all forms of my application,
and when it is removed from cradle, all the forms are enabled.
I search much. But the answer is not matched to my expect.
Please help me.
After reading reference of opennetcf, I found out the two events: ACPowerApplied and ACPowerRemoved
Here is my code:
public static event DeviceNotification ACPowerApplied;
public static event DeviceNotification ACPowerRemoved;
void Form1_ACPowerRemoved()
{
MessageBox.Show("Un-cradle");
}
void Form1_ACPowerApplied()
{
MessageBox.Show("Cradle");
}
private void Form1_Load(object sender, EventArgs e)
{
ACPowerApplied += new DeviceNotification(Form1_ACPowerApplied);
ACPowerRemoved += new DeviceNotification(Form1_ACPowerRemoved);
}
But the process did not step into Form1_ACPowerRemoved() and Form1_ACPowerApplied().
Is there any idea for that? Sorry for my poor English. Thank you in advance.
Your code is wrong. You've subscribed to the form's event, and nobody raise it.
Documentation doesn't show how-to-use code, I think. It shows declaraion.
Maybe it will work (not tested):
OpenNETCF.WindowsCE.DeviceManagement.ACPowerApplied += Form1_ACPowerApplied
OpenNETCF.WindowsCE.DeviceManagement.ACPowerRemoved += Form1_ACPowerRemoved
Also you can try to use WinAPI calls: http://blogs.msdn.com/b/davidklinems/archive/2005/02/10/370591.aspx
If you want, I have complete code, but there are a lot of waste and "OnRs232Connect" event.
By the way, what does "craddled" mean for you? Craddle can be disconneted from both AC and PC. I mean, do you want to handle when your device started to get electricity power, or when it started connecting to the PC via Active Sync? If the second, you want to catch "OnRs232Connect" event

how can you get a server callback on windows phone 8

I simply want to be able to make a callback on a function in the windows phone code from the server side when something is being updated there. Examples abound on the Internet make use of wsDualHttpBinding, but some wise man has decided to remove support for that from the windows store application API list. I can't find any other way to get the same functionality, does anybody know how to do this?
The reasoning behind not allowing for internet based callbacks is that it puts a big drain on the phone's battery to be constantly listening for them. Instead, they allow you to run PeriodicTask's that will let you occasionally call a server to poll whether there is a change.
Alternatively you could use their notification service:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402558%28v=vs.105%29.aspx
You can try something like this
System.Net.WebClient wc = new System.Net.WebClient();
public void Initialize(object sender, EventArgs e)
{
wc.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(done);
}
public string version = "1.0.0";
public void done(object sender, System.Net.DownloadStringCompletedEventArgs e)
{
if (version != e.Result)
{
//Do your code here
}
}
You can create a place online that stores the current version then check on start up.

Store events from other programs

I want to store events from other applications in my own app. Examples of events: when Word is opened, minimized or a file is opened.
Is such a thing possible?
Running a program and opening a file are OS events (they involve security checks), and can be captured using the Windows built-in auditing features. Those are off-topic here, direct further questions to ServerFault.com
Minimizing a program is an example of an application message, to get those you would need to install a hook using SetWindowsHookEx. Beginning in .NET 4 (which introduces parallel runtime support), this can be done with C#, but it's not recommended. One huge issue you need to be aware of is that your event capture code must NEVER generate events of its own, or else you will start a chain reaction that crashes all running programs.
Use EventLog.
You must set EnableRaisingEvents property to true
And When an event add to specified event log the
EntryWritten event handler will raise
It is the simplest way to handle os
events
sample code
private void frmMain_Load(object sender, EventArgs e)
{
System.Diagnostics.EventLog s = new System.Diagnostics.EventLog("Application", ".", "");
s.EnableRaisingEvents = true;
s.EntryWritten += delegate(object st, System.Diagnostics.EntryWrittenEventArgs ew)
{
MessageBox.Show(ew.Entry.Message);
};
}

How can my C# Windows app be notified of changes in network status without polling?

I would like to be notified when the computer's network connection is established (has a valid IP address) and I would like to do this without polling. Is there a Windows API that can provide these notifications?
I would start with the System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged event.
Also WMI events might be possible, fired on changes to the set of Win32_NetworkAdapter or Win32_NetworkConnection instances.
http://www.codeproject.com/KB/IP/usenetworklist.aspx
"How to use the Windows NLM API to get notified of new network connectivity"
public MainForm()
{
// Set listener to Check if Network Address Changed
NetworkChange.NetworkAddressChanged += new
NetworkAddressChangedEventHandler(AddressChangedCallback);
}
static void AddressChangedCallback(object sender, EventArgs e)
{
MessageBox.Show("Network Changed");
}

Categories

Resources