如何停止频繁调用函数

Roh*_*S D 2 c#

有一个功能可以用硬件密钥检查许可证.但是这个函数经常被调用并且需要时间来执行.所以为了避免太多的电话我想在一段时间后检查许可证.

bool CheckLicense()
{
    if(license checked in last 10 secconds)
    {
        return last status;
    }
    else 
    {
        hardware access for license check
        return current status
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:可能会删除硬件密钥,因此检查一次不是好习惯.还要调用许可证检查以启用和禁用不同的按钮状态.

Hak*_*tık 8

一般来说,我认为你需要这样的东西.

private DateTime lastCheckTime = DateTime.Now.AddDays(-1);

bool CheckLicense()
{
    if (lastCheckTime.AddSeconds(10) < DateTime.Now)
    {
        return last status;
    }
    else 
    {
        lastCheckTime = DateTime.Now;

        // hardware access for license check
        return current status   
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @RohanSD标记他的答案是正确的,如果帮助你! (2认同)