0

此代码只工作一次,即用户上线和下线时。我想以这样一种方式循环,它总是不断地为我提供信息。如:线上11:00、线下11:30、线上11:45、线下12:00。

我将如何解决这个问题?

bool showed =false;
bool nshowd =false;                         
for (; ; )
{
    //chech_online() <- this methods gives true if the person is online on FB

    bool check_online =check_online();                             

    if(check_online ==true && !showed)
    {
        Console.WriteLine("Online !!" +DateTime.Now);
        showed = true;
    } 
    else if(check_online ==false && !nshowd)
    {
        Console.WriteLine("OFFline !!" + DateTime.Now);
        nshowd = true;
    }
}
4

4 回答 4

2

为什么是showednshowed不同的变量?你不想要一个is_online变量然后证明它不同于check_online吗?

bool is_online = false;
for(;;)
{
    bool check_online = check_online();
    if (check_online != is_online)
    {
        // Online state has changed. Store the new state and log out
        is_online = check_online;
        if (is_online)
        {
            Console.WriteLine("Online !!" +DateTime.Now);
        }
        else
        {
            Console.WriteLine("Offline !!" +DateTime.Now);
        }
    }
}
于 2013-02-21T09:32:03.850 回答
1

由于showednshowd是互斥的,因此维护它们没有任何意义——这就是布尔变量的全部意义:

userShown = false
while true:
    userOnline = check_online()
    if userOnline && !userShown:
        output "User is online"
        userShown = true
    else:
        if !userOnline && userShown:
            output "User is offline"
            userShown = false
    sleep a bit

或者,您可以只保留最后一个状态,并且仅在它更改时显示:

isOnline = check_online()
wasOnline = !isOnline
while true:
    if isOnline != wasOnline:
        output "User is ", (isOnline) ? "online" : "offline"
        wasOnline = isOnline
    sleep a bit
    isOnline = check_online()

这种特殊的方法保证了初始消息的输出,而不管用户的状态如何。

于 2013-02-21T09:40:26.057 回答
0

你可以做一个循环,每次迭代睡十分钟等等?

bool toggle = false;
bool exitloop = false;
int checkinterval = 600; // time in seconds between check

while(!exitloop)
{
    bool is_online = check_online();                             

    if (is_online) 
    {
        if (!toggle) 
        {
            Console.WriteLine("Online!" + DateTime.Now.ToString();
            toggle = true;
        }
    }
    else
    {
        if (toggle) 
        {
            Console.WriteLine("Offline!" + DateTime.Now.ToString());
            toggle = false;
        }
    }

    Thread.Sleep(checkinterval * 1000);

    // it would be a good idea to allow a mechanism
    // to exit from the infinite loop.
    if (check_if_we_should_exit_loop()) 
    {
        exitloop = true;
    }
}
于 2013-02-21T09:32:48.347 回答
0
bool status = false;
        bool statusChange = false;
        while (true)
        {
            if (IsOnline())
            {
                if (status == false && statusChange == false)
                    statusChange = status = true;
                else if (status == false && statusChange == true)
                    status = true;
                else if (status == true && statusChange == false) ;
                else if (status == true && statusChange == true)
                    statusChange = false;
            }
            else
            {
                if (status == false && statusChange == false) ;
                else if (status == false && statusChange == true)
                    status = statusChange = false;
                else if (status == true && statusChange == false)
                {
                    status = false;
                    statusChange = true;
                }
                else if (status == true && statusChange == true)
                    statusChange = false;
            }
            if (statusChange)
            {
                UpdateStaus(status);
                statusChange= false;
            }
        }
于 2013-02-21T09:51:03.590 回答