Sensing when a network is available The code below just senses when the network is available and enables/disables a button on the UI. If I am registered to the NetworkAvailability event for the life of the program, will it cause a memory leak? I heard it might but didn't understand why.

System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged += new System.Net.NetworkInformation.NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);


delegate void EnableCallback();

void NetworkChange_NetworkAvailabilityChanged(object sender, System.Net.NetworkInformation.NetworkAvailabilityEventArgs e)
{
    EnableSync();
    //throw new NotImplementedException();            
}

private void EnableSync()
{
    if (this.btnSync.InvokeRequired)
    {
        EnableCallback methodCallback = new EnableCallback(EnableSync);
        this.Invoke(methodCallback, new object[] { });
    }
    else
    {
        if (btnSync.Enabled == true)
            btnSync.Enabled = false;
        else
            btnSync.Enabled = true;
    }
}
Well, if it's for the life of the program as you say, then no. When the program ends, all memory will be released.

Second, this all looks like one class. So root references will be removed when the class is GC'd and no memory leak will occur.

Lastly, write

        if (btnSync.Enabled == true)
            btnSync.Enabled = false;
        else
            btnSync.Enabled = true;


as

        btnSync.Enabled = !btnSync.Enabled;


Much more concise.