我正在使用Lync SDK 2013,并想检查客户端的连接。如果ClientState.SignedIn
为true,那么一切都很好,否则,我想启动一个计时器,直到状态返回true为止。
我的代码
public Client() // Initialize the timer in the constructor
{
connectionTimer = new Timer
{
Interval = 1000
};
connectionTimer.Tick += connectionTimer_Tick;
if (!IsSignedIn) // on application start
{
TryConnect();
}
}
private Timer connectionTimer;
public LyncClient Instance // get the current instance of the Lync Client
{
get
{
LyncClient client = null;
try
{
client = LyncClient.GetClient(); // get the client
if (client != null)
{
client.StateChanged += Client_StateChanged; // apply the state changed event to the client
}
}
catch (Exception)
{
}
return client;
}
}
public bool IsSignedIn // is the Lync client running?
{
get
{
bool instanceActive = Instance != null;
bool signedIn = false;
if (instanceActive)
{
signedIn = Instance.State == ClientState.SignedIn;
}
return signedIn;
}
}
public void TryConnect() // start the connection timer
{
connectionTimer.Start();
}
public void CheckConnection()
{
if (IsSignedIn) // is the lync client back online?
{
connectionTimer.Stop(); // stop the timer after reconnect
}
}
private void Client_StateChanged(Object source, ClientStateChangedEventArgs e)
{
if (e.NewState != ClientState.SignedIn) // the lync client is not signed in?
{
TryConnect();
}
}
private void connectionTimer_Tick(object sender, EventArgs e)
{
CheckConnection(); // check every second if the client is signed in
}
}
所以我真的很想知道如何在关闭Lync客户端时使此计时器再次运行。