简体   繁体   English

如何停止频繁调用函数

[英]How to stop function being called too often

There is a function which checks license with hardware key. 有一个功能可以用硬件密钥检查许可证。 But this function is getting called too often and takes time to execute. 但是这个函数经常被调用并且需要时间来执行。 So to avoid too many call I want to check license after sometime. 所以为了避免太多的电话我想在一段时间后检查许可证。

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

Edit: Hardware key might be removed so checking once is not good practice. 编辑:可能会删除硬件密钥,因此检查一次不是好习惯。 Also license check is to be called for enabling and disabling different button status. 还要调用许可证检查以启用和禁用不同的按钮状态。

In general, I think you would need something like this. 一般来说,我认为你需要这样的东西。

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   
    }
}

If you want to call it just once every 10 seconds you could use the following: 如果您想每10秒调用一次,则可以使用以下命令:

bool CheckLicense()
{
    bool currentStatus = false;

    //hardware access for license check

    new Thread(() =>
    {
        Thread.Sleep(10000);
        CheckLicense();
        }).Start();

    return currentStatus;
}

You call it once in your code and then every 10 seconds it will call itself. 你在代码中调用一次,然后每10秒调用一次。

Checking the licence every 10 seconds will definitely gonna add to many calls for the same function. 每隔10秒检查许可证肯定会增加许多相同功能的调用。 You can do it once when the program starts as suggested in the comments whereas if it is really necessary for you to check the license or calling a function after every some time you can actually increase the timings so that you know you have checked the licence and calls will be reduced. 您可以在评论中建议的程序启动时执行一次,而如果您确实需要检查许可证或在每隔一段时间后调用一个函数,您实际上可以增加时间,以便您知道已检查许可证和电话会减少。

Like for example you checked the licence for the first time when the program starts than after that about 10 seconds and then increase the timing by 10*2 which would be 20 than next time increase it by 20*2 which becomes 40 and this will lessens the call as well as you will be checking it every few times. 例如,您在程序启动时首次检查许可证的时间大于此时间约为10秒,然后将时间间隔增加10*2 ,这将比下次增加20*2增加20*2 ,这将减少电话以及您将每隔几次检查一次。

bool CheckLicense()
{
    timelimit = 300;
    if(seconds > timetocheck)
    {
        return last status;
        timetocheck *= 2;
        if(timetocheck >= timelimit)
        { 
           timetocheck = 10;
        }
    }
    else 
    {
        hardware access for license check
        return current status
    }
}

The program is just a prototype and doesnt mean to run directly it also does not talk about the datatypes and syntax. 该程序只是一个原型,并不意味着直接运行它也没有谈论数据类型和语法。 This is just for the understand-ability. 这只是为了理解能力。

If You are doing this checks in sync. 如果您正在同步进行此检查。 code, You might want to run new thread instead. 代码,您可能想要运行新线程。 And if there is problem with license, the separate thread will inform Your main thread through events: 如果许可证存在问题,单独的线程将通过事件通知您的主线程:

class LicenseChecker
{
    private Timer mTimer;
    public delegate void LicenseNotValidDelegate();
    public event LicenseNotValidDelegate LicenseNotValid;

    public LicenseChecker()
    {
        mTimer = new Timer();
        mTimer.Ticket += mTimer_Tick;
        mTimer.Interval = TimeSpan.FromSeconds(10);
    }

    public void Start()
    {
        mTimer.Start();
    }

    void mTimer_Tick(object sender, EventArgs e)
    {
        if(!CheckLicense())
            LicenseNotValid?.Invoke();
    }

    private bool CheckLicense()
    { ... }
}

...
public void Main()
{
    var lLC = new LicenseChecker();
    lLC.LicenseNotValid += lLC_LicenseNotValid;
    lLC.Start();
}

void lLC_LicenseNotValid()
{
    //code when license is not valid
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM