开发者

Re attempt server connection after some time

开发者 https://www.devze.com 2023-02-10 11:56 出处:网络
I have a client which is a windows process to get updates from server in c#. The server may be down for maintainance for some time, the client wont not 开发者_运维百科be able to connect to server at t

I have a client which is a windows process to get updates from server in c#. The server may be down for maintainance for some time, the client wont not 开发者_运维百科be able to connect to server at this point if it needs to update.

I want to design so that the client re attempts connection after 30 min if connection attempt fails. I understand Thread.Sleep would be bad idea so I would like some suggestions on how to do it.

Thanks in advance.


Use a timer. If the function fails to connect, then set a timer to call it again...

e.g.

    class MyClass
    {




 System.Timers.Timer m_ConnectTimer = null;

        ..
        ..

    void ConnecToServer()
    {
        if (m_ConnectTimer != null)
        {
            m_ConnectTimer.Enabled = false;
            m_ConnectTimer.Dispose();
            m_ConnectTimer = null;
        }

        //Try to connect to the server

        if (bConnectedToTheServer)
        {
            //Do the servery stuff
        }
        else //set the timer again
        {
            m_ConnectTimer = new Timer(30 * 60 * 1000);
            m_ConnectTimer.Elapsed += new ElapsedEventHandler(TimerHandler)
            m_ConnectTimer.Enabled = true;
        }

    }


    void TimerHandler(object sender, ElapsedEventArgs e)
    {
        ConnectToServer();
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号