private void OnStatusTimerExpired(object state)
{
try
{
// create the client if it has not already been created.
if (m_defaultClient == null)
{
m_defaultClient = CreateClient();
m_defaultClient.Key = String.Empty;
// set default locale.
if (AvailableLocaleIds != null && AvailableLocaleIds.Length > 0)
{
m_defaultClient.LocaleId = AvailableLocaleIds[0];
}
else
{
m_defaultClient.LocaleId = ComUtils.LOCALE_SYSTEM_DEFAULT;
}
m_defaultClient.CreateInstance();
m_lastStatusUpdate = DateTime.UtcNow;
}
// check if the last status updates appear to be hanging.
bool reconnected = false;
if (m_lastStatusUpdate.AddMilliseconds(m_statusUpdateInterval*1.1) < DateTime.UtcNow)
{
if (m_reconnecting)
{
return;
}
m_reconnecting = true;
try
{
Utils.Trace("Communication with COM server failed. Disposing server and reconnecting.");
// dispose existing client in the background in case it blocks.
ThreadPool.QueueUserWorkItem(OnDisposeClient, m_defaultClient);
// dispose localized clients.
if (m_localizedClients != null)
{
foreach (ComClient localizedClient in m_localizedClients.Values)
{
ThreadPool.QueueUserWorkItem(OnDisposeClient, localizedClient);
}
m_localizedClients.Clear();
}
// create a new client.
m_defaultClient = CreateClient();
m_defaultClient.CreateInstance();
reconnected = true;
}
finally
{
m_reconnecting = false;
}
}
// fetches the status from the server and updates the status node.
if (UpdateStatus())
{
AvailableLocaleIds = m_defaultClient.QueryAvailableLocales();
m_lastStatusUpdate = DateTime.UtcNow;
if (reconnected && m_reconnectCallback != null)
{
m_reconnectCallback(this);
}
}
}
catch (Exception e)
{
ComUtils.TraceComError(e, "Error fetching status from the COM server.");
}
}