Please consider below test program.
I have three tasks with specific interval. [task1-- task1Interval , [task2-- task2Interval , [task3-- task3Interval]
I want to use single timer to execute all three tasks.
Every thing works fine when interval is in integer.
we want to achieve the same functionality with double interval values for e.g [task1Interval-- 0.1 , [task2Interval-- 2.1 , [task3Interval-- 3.1].
Any pointers for the same would be highly appreciated.
public class Class1
{
private System.Timers.Timer _timer;
private int _counter=0;
private int task1Interval = 1;
private int task2Interval = 2;
private int task3Interval = 3;
public void Start()
{
this._timer = new System.Timers.Timer(100);
this._timer.AutoReset = true;
this._timer.Elapsed += new System.Timers.ElapsedEventHandler(this.serviceTimerElapse);
this._timer.Enabled = true;
this._timer.Start();
}
private void serviceTimerElapse(object source, System.Timers.ElapsedEventArgs e)
{
this._counter++;
if (this._counter % task1Interval == 0)
{
task1();
}
if (this._counter % task2Interval == 0)
{
task2();
}
if (this._counter % task3Interval == 0)
{
task3();
}
}
private void task1()
{
Console.WriteLine("task1 started");
}
private void task2()
{
Console.WriteLine("task2 started");
}
private void task3()
{
Console.WriteLine("task3 started");
}
}
This is very common to what you would see in a primitive game engine. You need a world "clock" with some level of granularity and you handle each event within each "tick".
You already known your granularity needs to be the least common denominator among your intervals (in this case 0.1). Then a primitive solution would include counters for each task along with a threshold.
Ex (pseudo code):
private const double TICK = 0.1;
private double TASK_1_THRESHOLD = 0.1;
private double TASK_1_COUNT = 0.0;
private double TASK_2_THRESHOLD = 2.1;
private double TASK_2_COUNT = 0.0;
private double TASK_3_THRESHOLD = 3.1;
private double TASK_3_COUNT = 0.0;
private void tick()
{
TASK_1_COUNT += TICK;
TASK_2_COUNT += TICK;
TASK_3_COUNT += TICK;
if (TASK_1_COUNT >= TASK_1_THRESHOLD) {
TASK_1_COUNT = 0.0;
task1();
}
// do this for each
}
Related
I am wondering what is the best way to achieve this in Windows Forms - what I need is a window showing time elapsed (1 sec 2 secs etc) up to 90 seconds while code is being executed. I have a timer right now implemented as follows but I think I also need a stopwatch there as well since the Timer blocks the main thread.
static System.Timers.Timer pXRFTimer = new System.Timers.Timer();
static int _pXRFTimerCounter = 0;
private void ScanpXRF()
{
_pXRFTimerCounter = 0;
pXRFTimer.Enabled = true;
pXRFTimer.Interval = 1000;
pXRFTimer.Elapsed += new ElapsedEventHandler(pXRFTimer_Tick);
pXRFTimer.Start();
//START action to be measured here!
DoSomethingToBeMeasured();
}
private static void pXRFTimer_Tick(Object sender, EventArgs e)
{
_pXRFTimerCounter++;
if (_pXRFTimerCounter >= 90)
{
pXRFTimer.Stop();
}
else
{
//show time elapsed
}
}
I'm not sure about mechanics of your app, but time elapsed can be calculated with something like this
DateTime startUtc;
private void ScanpXRF()
{
startUtc = DateTime.NowUtc;
(...)
//START action to be measured here!
}
private static void pXRFTimer_Tick(Object sender, EventArgs e)
{
var elapsed = DateTime.NowUtc - startUtc;
var elapsedSeconds = elapsed.TotalSeconds; // double so you may want to round.
}
First off, I am not using any kind of game engine, I am modding a game in C# and I am NOT using UnityEngine API so I do not have any Update() functions.
So I am trying to figure out how I could create a timer, some standard out of the box C# timer that would increase the lerp distance over a set speed.
model.rotationM = Vector3.Lerp(model.rotation, model.rotationM, (float)0.016);
NAPI.Entity.SetEntityRotation(model.handle, model.rotationM);
I would like to wrap this in a timer that every 100ms it will increase the float at the end of the lerp by some set amount over the duration of a time, so say I set float speed = 5f;
I want to increase that lerp distance every 100ms for 5 seconds until it reaches its goal.
Is this possible to do?
I've created an example timer class which will slowly increment a value by a given amount until it reaches 100% (1.0):
public class LerpTimer : IDisposable
{
private readonly Timer _timer;
private readonly float _incrementPercentage = 0;
public event EventHandler<float> DoLerp;
public event EventHandler Complete;
private bool _isDisposed = false;
private float _current;
public LerpTimer(double frequencyMs, float incrementPercentage)
{
if (frequencyMs <= 0)
{
throw new ArgumentOutOfRangeException(nameof(frequencyMs), "Frequency must be greater than 1ms.");
}
if (incrementPercentage < 0 || incrementPercentage > 1)
{
throw new ArgumentOutOfRangeException(nameof(incrementPercentage), "Increment percentage must be a value between 0 and 1");
}
_timer = new Timer(frequencyMs);
_timer.Elapsed += _timer_Elapsed;
_incrementPercentage = incrementPercentage;
}
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (_isDisposed)
{
return;
}
if (this.Current < 1)
{
this.Current = Math.Min(1, this.Current + _incrementPercentage);
this.DoLerp?.Invoke(this, this.Current);
}
if (this.Current >= 1)
{
this._timer.Stop();
this.Complete?.Invoke(this, EventArgs.Empty);
}
}
public float Current
{
get
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(LerpTimer));
}
return _current;
}
set => _current = value;
}
public void Start()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(LerpTimer));
}
if (_timer.Enabled)
{
throw new InvalidOperationException("Timer already running.");
}
this.Current = 0;
_timer.Start();
}
public void Stop()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(LerpTimer));
}
if (!_timer.Enabled)
{
throw new InvalidOperationException("Timer not running.");
}
_timer.Stop();
}
public void Dispose()
{
_isDisposed = true;
_timer?.Dispose();
}
}
Sample usage:
var lerpTimer = new LerpTimer(100, 0.016f);
lerpTimer.DoLerp += (sender, value) => {
model.rotationM = Vector3.Lerp(startRotation, endRotation, value);
NAPI.Entity.SetEntityRotation(model.handle, model.rotationM);
};
lerpTimer.Start();
So you would call this once, and then it would keep going until it reaches 100% (endRotation).
It's not necessarily the code you should use, but it should illustrate how you can use a timer to increase the value over time.
Edit to add some clarity to what a lerp function does:
double lerp(double start, double end, double percentage)
{
return start + ((end - start) * percentage);
}
Imagine we call this every 10% from 4 to 125. We would get the following results:
0% 4
10% 16.1
20% 28.2
30% 40.3
40% 52.4
50% 64.5
60% 76.6
70% 88.7
80% 100.8
90% 112.9
100% 125
Try it online
I'm trying to write a simple program to check battery status. I have a timer which ticks every second and I wrote some code, but I think it is not most effective.
1. I don't know if is the way, how I to check if is a percentage of battery is same as one second before, is right.
2. If I don't check MsgBox then percentage info in Label2 and MsgBox texts are not updated.
class Battery
{
public static int BatteryPercentage()
{
PowerStatus ps = SystemInformation.PowerStatus;
int percentage = Convert.ToInt32(ps.BatteryLifePercent * 100);
return percentage;
}
}
public static class MyClass
{
public static int LastPer { get; set; }
}
private void timer1_Tick(object sender, EventArgs e)
{
int per = Battery.BatteryPercentage();
label2.Text = per.ToString();
progressBar1.Value = per;
if (MyClass.LastPer == 0)
{
MyClass.LastPer = per;
}
else if (MyClass.LastPer > per && per <= 100)
{
MessageBox.Show("Battery is almost empty, remain " + per.ToString() + "!" );
}
}
I think it would be more efficient to get rid of your timer and subscribe to the report updated event of the Windows.Devices.Power.Battery class.
Something to the effect of
Battery.ReportUpdated += BatteryChargeLevelChanged;
public void BatteryChargeLevelChanged(object sender, EventArgs e)
{
// show your message if the charge is low
}
Trying to understand Timers and virtual clicks in C# Winforms. I want to have the program have an entered time value by the user (textbox1), then wait that amount of time and click the mouse, then increase the number counter (textbox2).
In the code below, the number counter immediately goes to 10, but the clicks are never ending, despite having a while loop set to stop the clicks at 10. I basically just want the program to wait a slightly random time (time entered to time entered +3), click the mouse, increase the counter, then pick a new random number and continue until 10 total clicks.
public Form1()
{
InitializeComponent();
}
private void NumbersOnly(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8)
{
e.Handled = true;
}
}
static System.Timers.Timer _timer;
int numberofclicks = 0;
[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
private const int MOUSEEVENTF_MOVE = 0x0001;
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
private const int MOUSEEVENTF_RIGHTUP = 0x0010;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
private void StartClicked(object sender, EventArgs e)
{
numberofclicks = 0;
Random rsn = new Random();
while (numberofclicks < 10)
{
string startseconds = textBox1.Text;
int timerstartseconds = Convert.ToInt32(startseconds);
int timertime = rsn.Next(timerstartseconds * 1000, ((timerstartseconds + 3) * 1000));
_timer = new System.Timers.Timer(timertime);
_timer.Elapsed += _timer_Elapsed;
_timer.Enabled = true;
textBox2.Clear();
numberofclicks++;
string numbertextbox = numberofclicks.ToString();
textBox2.Text = numbertextbox;
}
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
LeftClick();
}
public static void LeftClick()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
}
The problem lies in a fact that StartClicked event handler does not block itself. It is just a simple method that without any delay loops 10 times changing the method level variable and modifying(even creating new!) Timer properties.
It is possible to do what you attempted to do in such a manner(single method with simple loop), but you will have to use async event handler and will have no need for a timer. And as the another answer already discusses how to do it with classic timers, I will give you such async-based solution:
private async void StartClicked(object sender, EventArgs e)
{
numberofclicks = 0;
Random rsn = new Random();
while (numberofclicks < 10)
{
string startseconds = textBox1.Text;
int timerstartseconds = Convert.ToInt32(startseconds);
int timertime = rsn.Next(timerstartseconds * 1000, ((timerstartseconds + 3) * 1000));
await Task.Delay(timertime);
LeftClick();
textBox2.Clear();
numberofclicks++;
string numbertextbox = numberofclicks.ToString();
textBox2.Text = numbertextbox;
}
}
For more information on async-await you can read MSDN on async-await.
When you say _timer.Enabled = true;, the rest of your code keeps executing. Then at some time later, when the timer ticks, the Elapsed event is triggered.
Additionally, each timer you create keeps on ticking - they don't only fire once.
Also, there are many different Timer classes in the .NET framework. For a simple winforms app like yours, I would stick with the System.Windows.Forms.Timer version. You don't have to worry about threading and such that way.
You might be better served with something like this:
private Random rsn = new Random();
private int numberofclicks = 0;
private System.Windows.Forms.Timer _timer;
// set the timer's tick event handler in form_load or similar.
// you could also just drag a timer onto the form and double-click it.
private void StartClicked(object sender, EventArgs e)
{
// don't allow starting again until finished
btnStart.Enabled = false;
numberofclicks = 0;
_timer.Interval = /* get randomish interval */
_timer.Start();
}
void _timer_Tick(object sender, EventArgs e)
{
_timer.Stop();
LeftClick();
numberofclicks++;
if (numberofclicks >= 10) {
btnStart.Enabled = true;
}
else {
_timer.Interval = /* get randomish interval */
_timer.Start();
}
}
I'm very new to WP8 dev and c#. I'm trying to make a loop that counts up by n on an interval. I want to press a button to increment n.
Here is my code right now:
namespace Petsounds {
public partial class MainPage : PhoneApplicationPage {
float clicks = 0;
float clickers = 0;
float clickerBuyers = 0;
float clickerCost = 5;
float clickerBuyerCost = 500;
long savedTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;
bool buyClickerButtonFlag = false;
bool clickButtonFlag = false;
// Constructor
public MainPage() {
InitializeComponent();
//
DispatcherTimer t = new DispatcherTimer();
t.Interval = TimeSpan.FromMilliseconds(10);
t.Tick += (s, e) => startLoop();
t.Start();
}
private void clickButtonOnClick(object sender, RoutedEventArgs e) {
clickButtonFlag = true;
System.Diagnostics.Debug.WriteLine("clicked!" + clicks);
}
private void buyClickerButtonOnClick(object sender, RoutedEventArgs e) {
buyClickerButtonFlag = true;
}
private void startLoop() {
if (true) {
long nowTime = savedTime;
long timePassed = nowTime - savedTime;
//user input
if (clickButtonFlag) {
clickButtonFlag = false;
clicks++;
System.Diagnostics.Debug.WriteLine("clicked!" + clicks);
}
if (buyClickerButtonFlag) {
buyClickerButtonFlag = false;
if (clicks > clickerCost) {
clickers++;
clicks -= clickerCost;
clickerCost *= 1.6F;
}
System.Diagnostics.Debug.WriteLine("clicker bought!" + clickers);
}
//update vars
if (timePassed > TimeSpan.TicksPerSecond) {
savedTime = nowTime;
nowTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;
clicks += clickers;
}
//update display
clickCount.Text = clicks.ToString();
buyClickerButtonCost.Text = "Cossst " + clickerCost.ToString();
}
}
}
}
My button's are inconsistent, and if I remove the thread, the buttons are responsive (but of course the counter doesn't work.)
EDIT:
I've changed
DispatcherTimer t = new DispatcherTimer();
t.Interval = TimeSpan.FromMilliseconds(10);
t.Tick += (s, e) => startLoop();
t.Start();
to
Timer myTimer = new Timer(startLoop);
myTimer.Change(1000, 10);
And now get an error:
A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
on line
clickCount.Text = clicks.ToString();
First of all... you will quickly find that 10ms is not really 10ms... It might not even be that close... If you did 1000ms... that would be expected to be more accurate.
Also, a DispatcherTimer is going to queue up a function call to the GUI thread each interval... which means you are flooding the GUI thread with startLoop() calls. This doesn't give the thread much time to update anything else... like your buttons.
There is a different approach you might want to consider.
If your task is to increment a numeric value when a user touches a button (and have the numbers increase at a steady pace) consider using the RepeatButton.
RepeatButton: Represents a control that raises its Click event repeatedly from the time it is pressed until it is released.
XAML
<!--
Delay: The time, in milliseconds, the RepeatButton waits
when it is pressed before it starts repeating the click action.
Interval: The time, in milliseconds, between repetitions
of the click action, as soon as repeating starts.
-->
<RepeatButton Content='Buy'
Interval='50' Delay='100'
Click='RepeatButton_Click' />
Code
private float buyCounter = 0;
private void RepeatButton_Click(object sender, RoutedEventArgs e) {
buyCounter += 1;
buyClickerButtonCost.Text = buyCounter.ToString();
}
It's like #Andrew said - DispatcherTimer works on UI thread and with so small intervall you are blocking it.
If you want such a small interval you can use Timer on different Thread:
public MainPage()
{
InitializeComponent();
System.Threading.Timer myTimer = new Timer(MyTimerCallback);
myTimer.Change(1000, 10);
}
private static int value = 0;
private static void MyTimerCallback(object state)
{
value++;
}
But you must remember that you use it on different Thread - this Timer has no access to your UI elements (buttons and so on).
EDIT
You convinced me to check it:
static float clicks = 0;
static float clickers = 0;
static float clickerCost = 5;
static long savedTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;
static bool buyClickerButtonFlag = false;
static bool clickButtonFlag = false;
public MainPage()
{
InitializeComponent();
first.Click += ShowCounter;
DispatcherTimer t = new DispatcherTimer();
t.Interval = TimeSpan.FromSeconds(5);
t.Tick += ShowCounter;
t.Start();
System.Threading.Timer myTimer = new Timer(MyTimerCallback);
myTimer.Change(10, 10);
}
private void ShowCounter(object sender, EventArgs e)
{
textBlck.Text = clicks.ToString();
}
private static void MyTimerCallback(object state)
{
clicks++; // added to check running
if (true)
{
long nowTime = savedTime;
long timePassed = nowTime - savedTime;
//user input
if (clickButtonFlag)
{
clickButtonFlag = false;
clicks++;
System.Diagnostics.Debug.WriteLine("clicked!" + clicks);
}
if (buyClickerButtonFlag)
{
buyClickerButtonFlag = false;
if (clicks > clickerCost)
{
clickers++;
clicks -= clickerCost;
clickerCost *= 1.6F;
}
System.Diagnostics.Debug.WriteLine("clicker bought!" + clickers);
}
//update vars
if (timePassed > TimeSpan.TicksPerSecond)
{
savedTime = nowTime;
nowTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;
clicks += clickers;
}
}
}
I tested it on the device and buttons works.
On the other hand - what's the point of putting a method that waits for a flag buton click, when you can put the job easily to button click event. Let it happen when user clicked button - don't check buton state all the time.