G
Guest
OK..
I have a class that runs a process that basically does some connection
checking; I have a delegate setup to handle the status update event which
will allow the client to give some kind of indication to the GUI.
I get a NullReferenceException when I try to raise the event. Below is the
class source...
-------------START CODE-------------------
using System;
using System.Threading;
namespace SomeClass.ConnectionChecker
{
public class ConnectionManager : IDisposable
{
private bool _continuallyMonitorConnectionStatus = false;
private ConnectionStatus _currentConnectionStatus =
ConnectionStatus.UnknownStatus;
public ConnectionManager()
{
try
{
_currentConnectionStatus=_getCurrentConnectionStatus();
ThreadStart threadStartMethod = new ThreadStart(_monitorNetworkStatus);
Thread threadConnectionMonitor = new Thread(threadStartMethod);
_continuallyMonitorConnectionStatus=true;
threadConnectionMonitor.Start();
}
catch(Exception operationException)
{
_continuallyMonitorConnectionStatus=false;
throw new
ApplicationException("CommunicationsManager.CommunicationsManager(): " +
operationException.Message,operationException);
}
}
public delegate void ConnectionStateChanged(object sender,
ConnectionStatusEventArgs e);
public event ConnectionStateChanged OnConnectionStateChanged;
public delegate void ConnectionStateUpdate(object sender,
ConnectionStatusEventArgs e);
public event ConnectionStateUpdate OnConnectionStateUpdate;
private ConnectionStatus _getCurrentConnectionStatus()
{
ConnectionStatus currentConnectionStatus = ConnectionStatus.UnknownStatus;
try
{
//get the current status - raise status update event
//OnConnectionStateUpdate(this,new
ConnectionStatusEventArgs(currentConnectionStatus));
----ERROR OCCURS ON THIS LINE
OnConnectionStateUpdate(this,new
ConnectionStatusEventArgs(ConnectionStatus.UnknownStatus));
//if that status differs from the current status raise change event
if(currentConnectionStatus != _currentConnectionStatus)
{
OnConnectionStateChanged(this,new
ConnectionStatusEventArgs(currentConnectionStatus));
}
_currentConnectionStatus=currentConnectionStatus;
return ConnectionStatus.UnknownStatus;
}
catch(Exception privateMethodException)
{
_continuallyMonitorConnectionStatus=false;
throw new
ApplicationException("ConnectionManager._setCurrentNetworkStatus: " +
privateMethodException.Message,privateMethodException);
}
}
private void _monitorNetworkStatus()
{
while(_continuallyMonitorConnectionStatus)
{
_currentConnectionStatus=_getCurrentConnectionStatus();
}
}
public void Dispose()
{
_continuallyMonitorConnectionStatus=false;
}
public enum ConnectionStatus
{
/// <summary>The device has no outbound IP connection</summary>
NotConnected = 0,
/// <summary>
/// The device has an IP address but Internet availability has not or
cannot be
/// verified
/// </summary>
PartiallyConnected = 1,
/// <summary>The device has full bi-directional Internet
connectivity</summary>
FullyConnected = 2,
/// <summary>The device connectivity is in an unknown or indeterminate
state</summary>
UnknownStatus = 99
}
public class ConnectionStatusEventArgs : System.EventArgs
{
private ConnectionStatus _connectionStatus =
ConnectionStatus.UnknownStatus;
public ConnectionStatusEventArgs(ConnectionStatus connectionStatus) :
base()
{
_connectionStatus=connectionStatus;
}
public ConnectionStatus Status
{
get
{
return _connectionStatus;
}
}
}
}
}
-------------END CODE-----------------------
--------------------------------------------
Neil Macdonald
Senior Software Engineer
IntelliWhere Product Development Centre
Intergraph Corporation
Intergraph Mapping and Geospatial Solutions
I have a class that runs a process that basically does some connection
checking; I have a delegate setup to handle the status update event which
will allow the client to give some kind of indication to the GUI.
I get a NullReferenceException when I try to raise the event. Below is the
class source...
-------------START CODE-------------------
using System;
using System.Threading;
namespace SomeClass.ConnectionChecker
{
public class ConnectionManager : IDisposable
{
private bool _continuallyMonitorConnectionStatus = false;
private ConnectionStatus _currentConnectionStatus =
ConnectionStatus.UnknownStatus;
public ConnectionManager()
{
try
{
_currentConnectionStatus=_getCurrentConnectionStatus();
ThreadStart threadStartMethod = new ThreadStart(_monitorNetworkStatus);
Thread threadConnectionMonitor = new Thread(threadStartMethod);
_continuallyMonitorConnectionStatus=true;
threadConnectionMonitor.Start();
}
catch(Exception operationException)
{
_continuallyMonitorConnectionStatus=false;
throw new
ApplicationException("CommunicationsManager.CommunicationsManager(): " +
operationException.Message,operationException);
}
}
public delegate void ConnectionStateChanged(object sender,
ConnectionStatusEventArgs e);
public event ConnectionStateChanged OnConnectionStateChanged;
public delegate void ConnectionStateUpdate(object sender,
ConnectionStatusEventArgs e);
public event ConnectionStateUpdate OnConnectionStateUpdate;
private ConnectionStatus _getCurrentConnectionStatus()
{
ConnectionStatus currentConnectionStatus = ConnectionStatus.UnknownStatus;
try
{
//get the current status - raise status update event
//OnConnectionStateUpdate(this,new
ConnectionStatusEventArgs(currentConnectionStatus));
----ERROR OCCURS ON THIS LINE
OnConnectionStateUpdate(this,new
ConnectionStatusEventArgs(ConnectionStatus.UnknownStatus));
//if that status differs from the current status raise change event
if(currentConnectionStatus != _currentConnectionStatus)
{
OnConnectionStateChanged(this,new
ConnectionStatusEventArgs(currentConnectionStatus));
}
_currentConnectionStatus=currentConnectionStatus;
return ConnectionStatus.UnknownStatus;
}
catch(Exception privateMethodException)
{
_continuallyMonitorConnectionStatus=false;
throw new
ApplicationException("ConnectionManager._setCurrentNetworkStatus: " +
privateMethodException.Message,privateMethodException);
}
}
private void _monitorNetworkStatus()
{
while(_continuallyMonitorConnectionStatus)
{
_currentConnectionStatus=_getCurrentConnectionStatus();
}
}
public void Dispose()
{
_continuallyMonitorConnectionStatus=false;
}
public enum ConnectionStatus
{
/// <summary>The device has no outbound IP connection</summary>
NotConnected = 0,
/// <summary>
/// The device has an IP address but Internet availability has not or
cannot be
/// verified
/// </summary>
PartiallyConnected = 1,
/// <summary>The device has full bi-directional Internet
connectivity</summary>
FullyConnected = 2,
/// <summary>The device connectivity is in an unknown or indeterminate
state</summary>
UnknownStatus = 99
}
public class ConnectionStatusEventArgs : System.EventArgs
{
private ConnectionStatus _connectionStatus =
ConnectionStatus.UnknownStatus;
public ConnectionStatusEventArgs(ConnectionStatus connectionStatus) :
base()
{
_connectionStatus=connectionStatus;
}
public ConnectionStatus Status
{
get
{
return _connectionStatus;
}
}
}
}
}
-------------END CODE-----------------------
--------------------------------------------
Neil Macdonald
Senior Software Engineer
IntelliWhere Product Development Centre
Intergraph Corporation
Intergraph Mapping and Geospatial Solutions