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;
}
}
Related
I tried to hide a text in Unity after caps was pressed, but it doesn't work, it stops before "while".
I'm quite a not up to par programmer, so anyone more experienced?
private float TurnOffInfoText()
{
bool IsCapsPressed = Input.GetKeyDown(KeyCode.CapsLock);
while (IsCapsPressed == true)
{
EndOfGameText.enabled = false;
}
return 0;
}
Why does this even return a value?
Also your while loop would completely freeze the entire App and even the Unity Editor application! Within the loop the IsCapsPressed value is never ever changed!
I don't see where your method is called from but if you never experienced a freeze so far then "luckily" the key never went down in the same frame so far.
Usually you would rather poll the input every frame. By a simple look into the API for Input.GetKeyDown:
private void Update ()
{
if(Input.GetKeyDown(KeyCode.CapsLock))
{
EndOfGameText.enabled = false;
}
}
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
}
UPDATE: I've managed to fix my problem. Using the code below, I moved my MessageBox AFTER my XML saving and changed the Timer from 100ms to 400ms. I now have 1 box appear, thank god. Although If anyone has a short cut to updating a single value (ActReminded) in the List array(ActListTask), that'd be great to know.
I'm having a little issue with displaying the MessageBox. Show inside a timer without it spamming me. Here's the part of the code I've been working with:
public class ActiveTasks
{
//Properties here
}
public List<ActiveTasks> ActTaskList = new List<ActiveTasks>();
for (int i = 0; i < ListActive.Items.Count; i++)
{
if (DTime.Date == newDateTime.Date)
{
if (newDateTimeLeft.CompareTo(TimeSpan.Zero) <= 0 && ActTaskList[i].ActReminded != "true")
{
MessageBox.Show("!!!!");
ActTaskList.Add(new ActiveTasks()
{
ActTitle = ActTaskList[i].ActTitle,
ActDesc = ActTaskList[i].ActDesc,
ActDate = ActTaskList[i].ActDate,
ActTime = ActTaskList[i].ActTime,
ActStatus = ActTaskList[i].ActStatus,
ActReminded = "true",
ActRepeat = ActTaskList[i].ActRepeat
});
ListActive.Items.RemoveAt(i);
ActTaskList.RemoveAt(i);
XDocument XmlActTasks = GenerateActiveListToXML(ActTaskList);
}
}
}
I actually decided I may want to hold onto the reminder status, whether it has been shown or not as I wouldn't want a repeated reminder every time the program is opened. Since I don't know of a way to update an individual part of ActTaskList I just re-added it, and then deleted the original. This code manages to recognise that if it happens, it will change the reminder status from false, to true; after I've Ok'ed all the spam. So it will stop the MessageBox once I've managed to closed all the Messageboxes. However, it doesn't stop the spam. Would it be anything to do with the fact I've set the timer to 100ms? Or could their be an alternative way to make the messagebox appear without it being inside the timer?
The odds of the current time lining up exactly to the second what is happening in your loop is small. Why not treat newDateTime as a cut off point and just set a flag?
//Declare this outside of the loop
bool hasDisplayed = false;
//Inside the timer event handler
if (!hasDisplayed && DateTime.Now >= newDateTime)
{
hasDisplayed = true;
MessageBox.Show("!!!!!!!!!!!!!");
}
Can you do something like this?
Action message = () => MessageBox.Show("!!!!!!!!!!!!!"));
object lockOb = new object();
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
lock(lockOb)
if(null != message)
{
message();
message = null;
}
}
You say you've already tried a boolean indicating the message has already been shown, I'm assuming because the code probably looked like it did below.
void TimerLoop()
{
bool msgAlreadyShown;
if(!msgAlreadyShown)
{
MessageBox.Show("!!!!!!!");
}
// Other work in your timer function
}
The problem with that code is that the bool will be set to false each time the function is called by the timer. You haven't posted much code, but you've at least stated what you're trying to accomplish, a timer that checks if a reminder should be presented to the user.
I'm about to make some wild guesses about how you've put together your software, there's a good chance it's way off, but I hope it might point you in the right direction. You could have some sort of reminder class like this:
public class Reminder
{
string Message { get; set;}
DateTime Alarm { get; set; }
bool IsDismissed { get; set; }
}
I'm assuming you might want to have multiple reminders that can be checked for in the timer loop, so your timer loop could look something like:
private List<Reminder> _activeReminders; // A list of reminders
void TimerLoop(object s, ElapsedEventArgs e)
{
lock(_activeReminders)
{
var now = DateTime.Now;
foreach(var reminder in _activeReminders)
{
// only run this code if the time has passed and it hasn't already
// been shown
if(now.CompareTo(reminder.Alarm) >= 0 && !reminder.IsDismissed)
{
MessageBox.Show(reminder.Message);
reminder.IsDismissed = true;
}
}
}
}
This is a pretty naive implementation, since you probably don't want to hold onto the reminders for forever and the reminders are never removed from the _activeReminders list, but you essentially just need to add some sort of state to determine if the reminder has already been shown.
Of course, this isn't a complete example either, since I never new up the _activeReminders field or add anything to it, but I think this might help get the idea of what you need to do across. Also, you might not care about multiple reminders, and your timer code could look nothing like this. The main idea was to show you how you can keep track of the state of a reminder, and tailor it to your own code. The above was just an example.
Also, I haven't actually tested it, so treat it more like pseudocode than anything else. However, the logic is sound, and should it should only cause the message box to appear once.
I'm writing trading software and need to QoS one method that should not be executed more often than 10 times per second. As I'm begginer in C# and almost not familar with libraries I would like to double-check if my code is "optimal". I'm using Stopwatch because I don't know any other timers in C#.
Stopwatch updateStopwatch = Stopwatch.StartNew();
private void update()
{
if (updateStopwatch.ElapsedMilliseconds < 100)
{
Console.WriteLine("!skip update " + updateStopwatch.ElapsedMilliseconds);
return;
} else
{
Console.WriteLine("!update");
updateStopwatch.Restart();;
}
// do work here
}
upd Now it seems that Stopwatch is pretty good for this task. However probably it would be too slow, if so probably DateTime would be better. sell also Stopwatch vs. using System.DateTime.Now for timing events
Your technique of using Stopwatch is the best solution to prevent the code from executing more frequently. As others have said, using a Timer is a better solution if you want to make sure that the method is executed on a schedule.
Any approach based on DateTime is fundamentally broken because it will fail when the date changes. This is especially noticeable during the Daylight Saving Time switches. When we "spring ahead", there's the potential of the update running twice in quick succession because the code thinks that it's been an hour since the previous update. That's not too bad. But when we "fall back", the update will be suspended for a full hour because the last update time is set an hour ahead.
The same kind of thing can happen, although not as severely, if your computer is set to update its time periodically from an NTP server. If the time is set ahead, then there is the potential for two updates to happen in quick succession. If the time is set back, there's the potential for updates not to happen for the amount of time the clock was set back.
There are ways around the problem (such as using the absolute value of the number of milliseconds), but then you're just putting a bandage on a broken solution. You shouldn't depend on DateTime for intervals like this because your program isn't in control of the system clock--it can change at any time.
Stopwatch is the only reasonable solution here because it depends on the CPU's performance counter, which only increases. You don't have the problems of somebody setting the counter back, and you don't have the rollover problems you would encounter with something like Environment.TickCount.
There's some idea that Stopwatch incurs a performance penalty that DateTime doesn't. My testing shows that to be untrue.
Stopwatches and timers are fairly expensive objects to use. You could simply hold a DateTime object as a variable and perform a comparison.
DateTime lastCheck = DateTime.Now;
private void update()
{
// DateTime.Subtract returns a TimeSpan
int elapsed = DateTime.Now.Subtract(lastCheck).Milliseconds;
if (elapsed < 100)
{
Console.WriteLine("!skip update " + elapsed.ToString());
return;
} else
{
Console.WriteLine("!update");
lastCheck = DateTime.Now;
}
// do work here
}
I would not use a Stopwatch or anything other Timer-like. Instead just store the time of the method call and only execute the subsequent calls if the difference between the current and the stored time is bigger than 100ms.
You could implement a helper class to do this in a more general way:
public class TimedGate
{
private DateTime m_Last;
private TimeSpan m_Gap;
public TimedGate(TimeSpan gap)
{
m_Gap = gap;
}
public bool TryEnter()
{
DateTime now = DateTime.UtcNow;
if (now.Subtract(m_Last) > m_Gap)
{
m_LastEntered = now;
return true;
}
return false;
}
}
Use it like this:
TimedGate m_UpdateGate = new TimedGate(TimeSpan.FromMilliseconds(100));
private void Update()
{
if (m_UpdateGate.TryEnter())
{
Console.WriteLine("!update");
// do work here
}
else
{
Console.WriteLine("!skip update");
}
}
There is always the System.Timer timer.
That is probably easier to work with than the Stopwatch (which normally is used to measure how long time things take).
Code:
var timer = new System.Timers.Timer();
// Hook up the Elapsed event for the timer using a lambda
timer.Elapsed += (o, e) => Console.WriteLine("Timer elapsed");
// Set the Interval to 100 ms
timer.Interval = 100;
// Start the timer.
timer.Enabled = true;
MSDN docs: http://msdn.microsoft.com/en-us/library/system.timers.timer(v=VS.100).aspx
Let's say I have an existing System.Threading.Timer instance and I'd like to call Change on it to push it's firing time back:
var timer = new Timer(DelayCallback, null, 10000, Timeout.Infinite);
// ... (sometime later but before DelayCallback has executed)
timer.Change(20000, Timeout.Infinite);
I'm using this timer to perform an "idle callback" after a period of no activity. ("Idle" and "no activity" are application-defined conditions in this case...the specifics aren't terribly important.) Every time I perform an "action", I want to reset the timer so that it is always set to fire 10 seconds after that.
However, there is an inherent race condition because when I call Change, I can't tell if the Timer has already fired based on its old settings. (I can, of course, tell if my callback has happened but I can't tell if the CLR's internal timer thread has queued my callback to the threadpool and its execution is imminent.)
Now I know I can call Dispose on the timer instance and re-create it each time I need to "push it back". but this seems less efficient than just changing the existing timer. Of course it may not be...I'll run some micro-benchmarks in a bit and let you all know.
Alternatively, I can always keep track of the expected firing time (via DateTime.Now.AddSeconds(10)) and, if the original Timer fires, ignore it by checking DateTime.Now in the callback. (I have a nagging concern that this may not be 100% reliable on account of the Timer using TimeSpan and my check using DateTime...this may not be an issue but I'm not completely comfortable with it for some reason...)
My questions are:
Is there a good way for me to call Timer.Change and be able to know whether I managed to change it before the callback was queued to the threadpool? (I don't think so, but it doesn't hurt to ask...)
Has anyone else implemented (what I term) a "pushback timer" like this? If so, I'd love to hear how you tackled the problem.
This question is somewhat hypothetical in nature since I already have a couple of working solutions (based on Dispose and based on DateTime.Now)...I'm mainly interested in hearing performance-related suggestions (as I'll be "pushing back" the Timer VERY frequently).
Thanks!
it sounds like what you really want is the application-idle event
System.Windows.Forms.Application.Idle
Im interpreting your questions as a request for an implementatation of the IdleNotifier interface specified below. Also you state that ActionOccured() needs to be fast.
public delegate void IdleCallback();
public interface IdleNotifier
{
// Called by threadpool when more than IdleTimeSpanBeforeCallback
// has passed since last call on ActionOccured.
IdleCallback Callback { set; }
TimeSpan IdleTimeSpanBeforeCallback { set; }
void ActionOccured();
}
I provide an implementation with System.Threading.Timer below.
Important points about the implementation:
We accept that the timer can wake up at any time and make sure this is ok.
Since we assume the timer wakes relatively seldom we can do expensive work at these times.
Since we can do all logic in the timer callback all we need to do to "push the timer" is to remeber when last we pushed it.
Implementation:
public class IdleNotifierTimerImplementation : IdleNotifier
{
private readonly object SyncRoot = new object();
private readonly Timer m_Timer;
private IdleCallback m_IdleCallback = null;
private TimeSpan m_IdleTimeSpanBeforeEvent = TimeSpan.Zero;
// Null means there has been no action since last idle notification.
private DateTime? m_LastActionTime = null;
public IdleNotifierTimerImplementation()
{
m_Timer = new Timer(OnTimer);
}
private void OnTimer(object unusedState)
{
lock (SyncRoot)
{
if (m_LastActionTime == null)
{
m_Timer.Change(m_IdleTimeSpanBeforeEvent, TimeSpan.Zero);
return;
}
TimeSpan timeSinceLastUpdate = DateTime.UtcNow - m_LastActionTime.Value;
if (timeSinceLastUpdate > TimeSpan.Zero)
{
// We are no idle yet.
m_Timer.Change(timeSinceLastUpdate, TimeSpan.Zero);
return;
}
m_LastActionTime = null;
m_Timer.Change(m_IdleTimeSpanBeforeEvent, TimeSpan.Zero);
}
if (m_IdleCallback != null)
{
m_IdleCallback();
}
}
// IdleNotifier implementation below
public void ActionOccured()
{
lock (SyncRoot)
{
m_LastActionTime = DateTime.UtcNow;
}
}
public IdleCallback Callback
{
set
{
lock (SyncRoot)
{
m_IdleCallback = value;
}
}
}
public TimeSpan IdleTimeSpanBeforeCallback
{
set
{
lock (SyncRoot)
{
m_IdleTimeSpanBeforeEvent = value;
// Run OnTimer immediately
m_Timer.Change(TimeSpan.Zero, TimeSpan.Zero);
}
}
}
}
There are many straight-forward performance improvements on this code.
If anyone would be intrested in my first thoughts on this just ask me.
I've actually had to build my own "Timing" class for an MMORPG I've made. It could keep track of over 100,000 "entities" that had timers for processing AI, and other tasks. Based on different actions that could be taken, I would have to momentarily delay an event.
Now, my timing class was completely hand written, so it won't be exactly what you're looking for. But something that you could do that would be similar to the solution I came up with is to do a sort of:
while (sleepyTime > 0)
{
int temp = sleepyTime;
sleepyTime = 0;
Thread.Sleep(temp);
}
// here's where your actual code is.
Then, you can make a "Delay" method that basically just ads to sleepyTime.