Reputation: 514
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.
Upvotes: 2
Views: 1228
Reputation: 19421
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;
lastCheckTime = DateTime.Now;
// hardware access for license check
return current_status
}
Upvotes: 8
Reputation: 4371
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
}
Upvotes: 0
Reputation: 14044
Checking the licence every 10 seconds will definitely gonna add to many calls for the same function. 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.
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.
Upvotes: 0
Reputation: 1330
If you want to call it just once every 10 seconds you could use the following:
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.
Upvotes: 0