C
C#2y ago
Whiteboy

Timer not working

So i want a simple timer for my WPF app so i can run a Update each second but it sends an update every 2-3s instead how do i fix it?
private void StartUpdateLoop()
{
updateTimer = new Timer(Update, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
}
private void StartUpdateLoop()
{
updateTimer = new Timer(Update, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
}
public MainWindow()
{
InitializeComponent();
StartUpdateLoop();
}
public MainWindow()
{
InitializeComponent();
StartUpdateLoop();
}
private void Update(object? state)
{
elapsedTime++;
if (elapsedTime % 3 == 0)
{
IncreaseCurrentMoney(power1Output);
}
if (elapsedTime % 9 == 0)
{
IncreaseCurrentMoney(power2Output);
}
if (elapsedTime % 27 == 0)
{
IncreaseCurrentMoney(power3Output);
}
if (elapsedTime % 81 == 0)
{
IncreaseCurrentMoney(power4Output);
}
if (elapsedTime % 273 == 0)
{
IncreaseCurrentMoney(power5Output);
}
}

private void IncreaseCurrentMoney(double amount)
{
lock (this)
{
currentMoney += amount;
LbCurrentMoney.Content = $"{currentMoney}$";
}
}
private void Update(object? state)
{
elapsedTime++;
if (elapsedTime % 3 == 0)
{
IncreaseCurrentMoney(power1Output);
}
if (elapsedTime % 9 == 0)
{
IncreaseCurrentMoney(power2Output);
}
if (elapsedTime % 27 == 0)
{
IncreaseCurrentMoney(power3Output);
}
if (elapsedTime % 81 == 0)
{
IncreaseCurrentMoney(power4Output);
}
if (elapsedTime % 273 == 0)
{
IncreaseCurrentMoney(power5Output);
}
}

private void IncreaseCurrentMoney(double amount)
{
lock (this)
{
currentMoney += amount;
LbCurrentMoney.Content = $"{currentMoney}$";
}
}
11 Replies
JakenVeina
JakenVeina2y ago
what makes you say that? why in the world do you have a lock() there?
cap5lut
cap5lut2y ago
in WPF u shouldnt use the Timer but the DispatcherTimer, especially because it seems like u r updating gui elements on their ticks (see the Remarks section of the link) https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatchertimer?view=windowsdesktop-7.0
DispatcherTimer Class (System.Windows.Threading)
A timer that is integrated into the Dispatcher queue which is processed at a specified interval of time and at a specified priority.
Whiteboy
WhiteboyOP2y ago
so i made some changes
public partial class MainWindow
{
private double currentMoney = 0;
private double moneyPerSec = 0.01;
private double elapsedTime = 0.0d;

private DispatcherTimer timer;

public MainWindow()
{
InitializeComponent();
SetInitialValues();
StartUpdateLoop();
}

private void StartUpdateLoop()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
timer.Start();
}

private void Timer_Tick(object? sender, EventArgs e)
{
elapsedTime++;
Update();
}

private void Update()
{
if (elapsedTime % 3 == 0)
{
IncreaseCurrentMoney(power1Output);
}
if (elapsedTime % 9 == 0)
{
IncreaseCurrentMoney(power2Output);
}
if (elapsedTime % 27 == 0)
{
IncreaseCurrentMoney(power3Output);
}
if (elapsedTime % 81 == 0)
{
IncreaseCurrentMoney(power4Output);
}
if (elapsedTime % 273 == 0)
{
IncreaseCurrentMoney(power5Output);
}
}

private void IncreaseCurrentMoney(double amount)
{
currentMoney += amount;
LbCurrentMoney.Content = $"{currentMoney}$";
}
}
public partial class MainWindow
{
private double currentMoney = 0;
private double moneyPerSec = 0.01;
private double elapsedTime = 0.0d;

private DispatcherTimer timer;

public MainWindow()
{
InitializeComponent();
SetInitialValues();
StartUpdateLoop();
}

private void StartUpdateLoop()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
timer.Start();
}

private void Timer_Tick(object? sender, EventArgs e)
{
elapsedTime++;
Update();
}

private void Update()
{
if (elapsedTime % 3 == 0)
{
IncreaseCurrentMoney(power1Output);
}
if (elapsedTime % 9 == 0)
{
IncreaseCurrentMoney(power2Output);
}
if (elapsedTime % 27 == 0)
{
IncreaseCurrentMoney(power3Output);
}
if (elapsedTime % 81 == 0)
{
IncreaseCurrentMoney(power4Output);
}
if (elapsedTime % 273 == 0)
{
IncreaseCurrentMoney(power5Output);
}
}

private void IncreaseCurrentMoney(double amount)
{
currentMoney += amount;
LbCurrentMoney.Content = $"{currentMoney}$";
}
}
and it still goes every 2-3s not 1s
cap5lut
cap5lut2y ago
wel,, u increment by one, so every 2 out of 3 ticks nothing happens in ur Update() method 1. tick = elapsedTime = 1 -> nothing happens 2. tick = elapsedTime = 2 -> nothing happens 3. tick = elapsedTime = 3 -> IncreaseCurrentMoney(power1Output) will be called 4. tick = elapsedTime = 4 -> nothing happens 5. tick = elapsedTime = 5 -> nothing happens 6. tick = elapsedTime = 6 -> IncreaseCurrentMoney(power1Output) will be called and so on
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut2y ago
thats +1 not +10, its power<number>Output ;p
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut2y ago
thats power one output for % 3 == 0, not power ten, for % 9 its two not twenty.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut2y ago
well, yeah most likely they want an array for that ;p
Whiteboy
WhiteboyOP2y ago
yeah... idk why it took me so long to get it everything was good ye i remade it with if elses
Want results from more Discord servers?
Add your server