计时器仅启动一次[C#/ Lync SDK]

时间:2018-08-16 08:13:01

标签: c# events lync skypedeveloper lync-client-sdk

我正在使用Lync SDK 2013,并想检查客户端的连接。如果ClientState.SignedIn为true,那么一切都很好,否则,我想启动一个计时器,直到状态返回true为止。

  • 我没有运行Lync客户端
  • 我启动WinForms应用程序
  • 应用程序显示“离线”
  • 计时器开始运行(尝试重新连接)
  • 我启动Lync客户端
  • 应用程序显示“在线”
  • 计时器停止
  • 我关闭Lync客户端
  • 计时器不会再次启动

我的代码

    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客户端时使此计时器再次运行。

0 个答案:

没有答案