How to stop function being called too often - c#

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;
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:
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.

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.

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
}

Related

Best solution for stopping a piece of code to be called multiple times?

I have a method which is been called every 1 hour. But sometimes the method operations are not been completed fully within one hour and the method is again been called which causes confusion. So I have to find out if the previous method is been completed. Which is the best solution for this problem?
// will be called for every one hour where the value will be repeated
// At sometimes it is possible for the same value to be called continually
for the next hour and at that time problem occurs
Void Method(int value)
{
If(value =0)
// Do some operations which may exceed one hour
Else If(value =1)
// Do some operation’s which may exceed one hour
.
.
.
}
Thanks,
One question would be what do you want to happen if the method is called while it is still running?
This code will just drop the 2nd call
private bool _running = false;
private readonly object _lock = new object();
void Method(int value)
{
lock (_lock)
{
if (_running)
{
return;
}
else
{
_running = true;
}
}
if (value == 0)
{
// Do some operations which may exceed one hour
}
else if (value == 1)
{
// Do some operation’s which may exceed one hour
}
_running = false;
}
A simple idea is to save the state in a class field, so that the method checks to see if it's free to do some work or not. This will mean that if you call the method and it's busy, the work won't happen for your call:
private static bool methodIsBusy = false;
private static void WaitAndWriteMessage(TimeSpan waitTime, string message)
{
// If we're busy, return right away
if (methodIsBusy) return;
// Let future calls know we're busy
methodIsBusy = true;
Thread.Sleep(waitTime);
Console.Write($"Method ran at: {DateTime.Now.ToString("hh:mm:ss")}. ");
Console.WriteLine(message);
// Allow future calls to run now
methodIsBusy = false;
}
Our test method:
private static void Main()
{
for(int i = 0; i < 3; i++)
{
Task.Run(() => WaitAndWriteMessage(TimeSpan.FromSeconds(5),
$"Method called at {DateTime.Now.ToString("hh:mm:ss")}."));
Thread.Sleep(1000);
}
Console.ReadKey();
GetKeyFromUser("\nDone!\nPress any key to exit...");
}
Output
(Only the first of the three messages was printed)
Another idea is to use a lock, which means that calls to the method will pile up if the method is busy. All calls will eventually get completed, but if every call is longer than the iteration time in which the method is called, it will get further and further behind:
private static object methodLocker = new object();
private static void WaitAndWriteMessage(TimeSpan waitTime, string message)
{
// Lock on a common object, so this call will wait
// until there are no locks before it can continue
lock (methodLocker)
{
Thread.Sleep(waitTime);
Console.Write($"Method ran at: {DateTime.Now.ToString("hh:mm:ss")}. ");
Console.WriteLine(message);
}
}
Our test method again:
private static void Main()
{
for(int i = 0; i < 3; i++)
{
Task.Run(() => WaitAndWriteMessage(TimeSpan.FromSeconds(5),
$"Method called at {DateTime.Now.ToString("hh:mm:ss")}."));
Thread.Sleep(1000);
}
Console.ReadKey();
}
Output
(Notice the difference between when the message was called and when it executed gets longer each time)

Restarting a method

I am making a C# aplication. I have got one main process for verifying data from a database. A timer checks every 100 ticks if I have user input. After I get user input my main process continues (it waits for userinput at the start). After verifying and doing multiple things the method is done. The thing is, I would like it to go to the beginning again waiting for the next bit of input.
I was thinking of calling the method again at every possible end of the process. I have a feeling that this will create a resource heavy program though (not the worst thing, but better no bad habits than a few right?).
Example:
bool cont = false;
public void process()
{
while (cont == false) {;}
//Various sorting criteria that all end up with cont = false; process(), the userinput has been processed.
}
timer1 tick event
{
if (userinput)
cont = true;
}
As you don't saw how you will get the user input, i don't implemented this one. But the main logic to your question is :
class MainClass
{
public static void Main()
{
MyRecursiveFunction();
AfterUserInput();
}
public static void MyRecursiveFunction()
{
if (userinput)
{ return; }
// Waits 100 ticks to check again
Thread.Sleep(new TimeSpan(100));
MyRecursiveFunction();
}
public static void AfterUserInput()
{
// All that you need to do after the user input
}
}

Backgroundprocess for timebased operations

i have an application (.Net Framework 2.0!) where you can enter operations which will be executed at a given time.
Now i want to create a process which runs in background and does nothing else then waiting till the given time is reached and call the operation to run. The application should run things like making backup of specific parts of the computer, start updates, run batches, ... The backgroundworker will run over several month doing nothing else.
Using the code below would work but it seems a bit ugly. Is there any better solution?
while(true && !applicationClosing)
{
List<ExecProcess> processList = LoadStoredProcesses();
List<ExecProcess> spawnedProcesses = new List<ExecProcess>();
DateTime actualTime = DateTime.Now();
foreach(ExecProcess ep in processList)
{
if(ep.ExecutionTime < actualTime)
{
ep.Execute();
spawnedProcesses.Add(ep);
}
}
RemoveSpawnedProcesses(spawnedProcesses);
Thread.Sleep(1000);
}
Thank you verry much.
I would suggest using a Windows service which implements a timer that fires an event every n seconds. You can pickup your tasks from wherever you want, and queue them internally in the service and fire at given times. Just check the timestamps within the _executeTimer_Elapsed method. This is only a small sample, but it should be enough to get you started.
public class MyService : ServiceBase
{
private Timer _executeTimer;
private bool _isRunning;
public MyService()
{
_executeTimer = new Timer();
_executeTimer.Interval = 1000 * 60; // 1 minute
_executeTimer.Elapsed += _executeTimer_Elapsed;
_executeTimer.Start();
}
private void _executeTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (!_isRunning) return; // logic already running, skip out.
try
{
_isRunning = true; // set timer event running.
// perform some logic.
}
catch (Exception ex)
{
// perform some error handling. You should be aware of which
// exceptions you can handle and which you can't handle.
// Blanket handling Exception is not recommended.
throw;
}
finally
{
_isRunning = false; // set timer event finished.
}
}
protected override void OnStart(string[] args)
{
// perform some startup initialization here.
_executeTimer.Start();
}
protected override void OnStop()
{
// perform shutdown logic here.
_executeTimer.Stop();
}
}

Make something happen when a boolean is changed, but not changed back

I need to make it so that when my boolean value changes to whatever it wasn't, something happens for a couple seconds and then stops, even if the boolean didn't change back.
I am yet to figure out a way to make something happen for a few seconds. The check is also happening constantly so as soon as the boolean changes, the movement starts and doesn't stop.
I might be sounding unclear so feel free to ask more specific questions...
EDIT: Cause a couple of people asked, I specifically need some of my game objects to move downwards for a couple seconds then stop and go back to moving horizontally (think Space Invaders).
You should look into properties, by using a public accessor which handles logic, and a private backing field.
public bool Enabled
{
get {
return enabled;
}
set {
if (value != enabled)
{
//Do Something if it changes
}
enabled = value;
}
}
private bool enabled;
The get code is executed when you get the property (Ex: bool isEnabled = Enabled), and the set is called when you set the property (Ex: Enabled = false) Note that the value is the new value being passed when you assign something to the property.
Now that you know how to check and execute code when the value changes, we can move on to the "something" you would like to do for a few seconds.
You can use a Timer found in System.Timers (Tutorial) to run code at an interval (And stop it after X intervals), or you can run code repeatedly using a StopWatch until it hits a certain time. Examples provided below,
Timer timer = new Timer(3000);
timer.Elapsed += (object sender, ElapsedEventArgs e) => { Console.WriteLine("Something"); };
timer.Enabled = true;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Restart();
while (true)
{
//If 3 seconds have elapsed, stop the operation
if (stopWatch.ElapsedMilliseconds > 3000)
{
stopWatch.Stop();
break;
}
//Do stuff repeatedly
Console.WriteLine("Something");
}
All of that code can be put into a method and called from the set block. (And if you need to use the value of the property, you should pass it as an argument, otherwise you might run into issues if it changes)
EDIT: I did not see this was an XNA game, in that case, you need to set the time when you started moving, and keep moving until the current time is more than the start time, plus X amount:
Example:
Have a 3rd value called enabledChanged or whatever, that you can use later in your Update method so you know to start moving. Get the time when you start moving and set the changed flag back to false. Then keep moving until the current GameTime is past the desired time.
private bool enabledChanged;
private double startTime;
...
protected void Update(GameTime gameTime)
{
if (enabledChanged) //If we should start moving
{
enabledChanged = false;
startTime = gameTime.TotalGameTime.TotalSeconds;
}
if (gameTime.ElapsedGameTime.TotalSeconds <= startTime + 3) //If time is less than start time, plus 3 seconds, move
{
//Move object
}
}
It really depends on what you want to do, but this is a general solution. (Because do you mean run something until time ends, completely stoping the operation? Or to repeat an action until time ends?)
Well, you can save the last value of the boolean, then if it doesn't equal to the current value then set a new boolean value to true.
bool myBool = false;
bool lastValue = myBool;
bool doRunning = false;
// Your loop
while (true) {
.....
if (myBool != lastValue) {
doRunning = true;
lastValue = myBool;
}
}

Increment a Temporary / Timed Variable in C#

Forgive the ignorance of this question, as I'm completely new to C#, but how would one go about setting and handling a temporary or timed variable? For example, whenever an event fires, I want to increment a counter. If the counter exceeds 3 within 60 seconds, I want to trigger another event. Elsewise, if 60 seconds passes and the counter isn't incremented, it resets back to 0.
Should I be using MemoryCache? Or is there some nifty flag I can set for variables to make them unset after a specified duration (assuming they aren't refreshed)? What would be the best way to go about accomplishing this simple task? Note that I'm not necessarily looking for someone to write it for me; just a helpful hand or suggestion to point me in the right direction. This is a rough outline of what I'm trying to accomplish:
private static int totalCount = 0;
private static double maxCount = 3;
private static double timeLimit = 60;
private static void TestEvent(object src, EventArgs mea)
{
totalCount++;
if (totalCount > maxCount)
{
DoSomething();
}
}
Thanks for any assistance you can offer. I make it a point to always reward helpful answers.
You could maintain a Queue<T> (see http://msdn.microsoft.com/en-us/library/7977ey2c.aspx), where each entry is the time that an event fired. When an event fires, you first remove from the queue any entries that are more than 60 seconds old (which is easy, because the queue is ordered by time, and the head of the queue is the oldest entry), then add a new entry for the event that just fired. If the count of the queue exceeds your threshold, then you have satisfied the condition you're looking for.
I recommend using DateTime.UtcNow instead of DateTime.Now for the time you store in the Queue<T>. UtcNow is much faster, and it avoids the problem that transitions from daylight savings time to standard time and vice versa can cause.
Here is some code off the top of my head (may need a little fixing up):
private static Queue<DateTime> eventQueue = new Queue<DateTime>();
private static int timeWindowSeconds = 60;
private static int threshold = 3;
private static void TestEvent(object src, EventArgs mea) {
DateTime now = DateTime.UtcNow;
DateTime tooOld = now.AddSeconds(-timeWindowSeconds);
// remove old entries
while((eventQueue.Count > 0) && (eventQueue.Peek() < tooOld)) {
eventQueue.Dequeue();
}
// add new entry
eventQueue.Enqueue(now);
// test for condition
if (eventQueue.Count >= threshold) {
eventQueue.Clear();
DoSomething();
}
}
You might do it like this:
private static int totalCount = 0;
private static double maxCount = 3;
private static TimeSpan timeLimit = TimeSpan.FromSeconds(60);
private static DateTime lastIncrementTime;
private static void TestEvent(object src, EventArgs mea)
{
// If the time between now and lastIncrementTime is more than the timeLimit...
if(DateTime.Now - lastIncrementTime > timeLimit)
{
totalCount = 0;
}
lastIncrementTime = DateTime.Now;
totalCount++;
if (totalCount > maxCount)
{
DoSomething();
}
}
You can use the StopWatch class.
On the form load event (if you want to count the 60 second from that event), start the timer,
Everytime in the click event,check how many seconds it is invoked your other method or so.
I'd do something like this:
private class SomeEventMonitor
{
public int Threshold { get ; private set ; }
public TimeSpan ThresholdWindow { get ; private set ; }
private DateTime marker ;
private int count ;
/// <summary>
/// public constructor
/// </summary>
/// <param name="threshold"></param>
/// <param name="window"></param>
public SomeEventMonitor( int threshold , TimeSpan window )
{
if ( threshold < 1 ) throw new ArgumentOutOfRangeException("threshold") ;
if ( window <= TimeSpan.Zero ) throw new ArgumentOutofRangeException("window") ;
this.Threshold = threshold ;
this.ThresholdWindow = window ;
Reset() ;
return ;
}
private void Reset()
{
this.marker = DateTime.Now ;
this.count = 0 ;
return ;
}
public event EventHandler ThresholdExceeded ;
private static readonly object latch = new object() ;
public void EventWatcher( object source , EventArgs eventArgs )
{
lock ( latch )
{
DateTime current = DateTime.Now ;
if ( ++count > Threshold )
{
TimeSpan window = current -marker ;
if ( window > ThresholdWindow )
{
ThresholdExceeded( this , new EventArgs() ) ;
Reset() ;
}
}
}
return ;
}
}
You can use another variable to log the time of changing the value of totalcount. check it with the current time and do whatever you want.
Here is the code...
private static int totalCount = 0;
private static double maxCount = 3;
private static double timeLimit = 60;
private static DateTime timeflag= DateTime.Now;
private static void TestEvent(object src, EventArgs mea)
{
if (timeflag.AddSeconds(timeLimit) < DateTime.Now)
{
totalCount = 0;
}
totalCount++;
timeflag = DateTime.Now;
if (totalCount > maxCount)
{
DoSomething();
}
}
There are myriad ways you could approach this. The first thing that comes to mind for me would be to use a Stopwatch object or a Timer object that starts on a background thread, and then write an event handler that can subscribe to the event in which you're interested. As the event occurs, your handler fires, allowing you to suspend the timer and query the time elapsed, and make your increment/reset decision accordingly.
That's merely a very rough sketch of one notion, but should give you some ideas moving forward. Good luck.
Per the comment made by #hatchet above, this almost starts to sound like a queue with "expiring" members or a "sliding window" event horizon you'd have to capture should that comment accurately reflect your problem.
EDIT Being the borderline obsessive-compulsive that I am, I gave your original problem some thought and came up with a concept that may or may not be relevant to your problem, but at least for the academic exercise I'm going to post what I did. What caught my attention in your original post was the notion of an expiring or timed variable which I thought was quite novel. Your problem specified that you want to do something specific when a given interval elapses.
I tried to abstract that idea into a generic form, thinking of a few ways such an idea might be useful. One idea that came to mind was in a game environment, where (for example) a message might only be available to the player for 20 seconds before "self-destructing." I could imagine how having the expiration "plumbing" wired into a type might prove very convenient. Another scenario could be in a CAI environment where a System.Drawing.Image should only be displayed for a fixed time, then disappear - again, a scenario where having the expiration and timing code built-in could be useful.
So, with at least that much notional practicality in mind, I set to work, and what I threw together (and I won't pretend that its comprehensive or complete) is a generic for an Expiring type, expressed as Expiring<T>. The baseline code I've put together is as follows:
// First stab at an "expiring" type that is only valid for a set interval.
public class Expiring<T>
{
public delegate void ExpiredHandler(object sender, EventArgs e);
public event ExpiredHandler OnExpired;
T instance;
int signaledCount = 0;
long milliseconds = 0;
bool isExpired = false;
bool exceptOnExpiredReference = true;
System.Timers.Timer lapseTimer = new System.Timers.Timer();
public Expiring(T value)
{
instance = value;
}
public virtual void TimerElapsed(object sender, System.Timers.ElapsedEventArgs args)
{
if (OnExpired != null)
{
OnExpired(this, null);
}
isExpired = true;
}
public Expiring(T startValue, long expirationInterval, bool throwElapsedReferenceException):this(startValue)
{
milliseconds = expirationInterval;
lapseTimer.AutoReset = true;
lapseTimer.Interval = milliseconds;
exceptOnExpiredReference = throwElapsedReferenceException;
lapseTimer.Elapsed+=new System.Timers.ElapsedEventHandler(TimerElapsed);
this.Set();
}
public void Set()
{
signaledCount++;
lapseTimer.Stop();
lapseTimer.Start();
}
public T Value
{
get
{
if (!isExpired || !exceptOnExpiredReference)
return instance;
else
throw new InvalidOperationException("Reference to an expired value.");
}
set
{
instance = value;
}
}
}
The idea here is that someone could declare an Expiring<int>, specify its initial value, expiration time, and a value to indicate whether an attempt to access the value of an instance after the expiration interval has passed should throw an exception. When the expirationInterval passes, the OnExpired event is raised, allowing the declarer to specify a custom event handler to provide custom actions when the value expires.
If the caller wishes to reset the expiration timer, he need only call the Set() method of the object. That also increments an internal "signaledCount" value that I ultimately did not use, but was thinking of in terms of determining how many times the expiration timer has been reset. If the Value property of the object is accessed after the expiration interval passes, an InvalidOperationException is thrown with a "Value has expired" message.
This idea, unfortunately, has more notional/academic value than practical, I'm afraid. It would have a great deal more utility if it were possible to overload all the arithmetic operators to the implementations of the native value types, but I discovered quickly that C# doesn't like this notion at all (and found that out right here on a rather extensive post on the subject here on SO). Ideally, I'd love to be able to say something like:
Expired<Int32> foo = new Expired<Int32>(5,10000,true);
Expired<Int32> bar = new Expired<Int32>(10,10000,true);
Expired<Int32> baz = foo+bar; // can't make that work
There was some notion that this problem could be overcome with dynamic types, but I opted not to pursue it at this point. The idea, as I hammered it out, is offered for discussion as it applies to a generic view of the OP's "timed variable" notion. Constructive comments/criticism/refinements encouraged and welcome.

Categories