0

I'm currently working on a UWP project and need some help with my DispatcherTimer.

What I'm working with is a booking function and I want a color to change if there is 15 seconds left until the DispatcherTimer ticks.

I have two variables that is named timefrom and timeTo and they come from a SQL database. So what I want to do is to first change a color from Green to Red and then stay Red until there is 15 seconds left until the timeto = DateTime.UtcNow

"timefrom": "2018-02-17T14:00:00"
"timeto": "2018-02-17T15:15:00"

Right now, what I have tried with is this:

if (timeto < DateTime.UtcNow)
{
    StatusColor.Fill = GreenBrush;
}
else if (timeto > DateTime.UtcNow)
{
    GreenToRed();
}

This is my DispatcherTimer:

public void GreenToRed()
        {
            DispatcherTimer ColorTimer = new DispatcherTimer();
            ColorTimer.Interval = TimeSpan.FromSeconds(3);
            ColorTimer.Tick += async (Sender, args) =>
            {
                await StatusColor.Fade(duration: 1000, delay: 0, value: 0).StartAsync();
                StatusColor.Fill = RedBrush;
                await StatusColor.Fade(duration: 1200, delay: 0, value: 1).StartAsync();
                RedToYellow();
                ColorTimer.Stop();
            };
            ColorTimer.Start();
        }
  • StatusColor is a retangle that is suposed to contain the color as a fill.
  • GreenBrush, RedBrush and YellowBrush is the three colors that I want to switch between.

Thanks in advance!

luddep
  • 219
  • 1
  • 2
  • 14
  • What trouble are you experiencing? Using timers or calculating when it should fire? – Enigmativity Jun 11 '18 at 09:12
  • I mean the problems I get is that it is not working, there is no errors at all but right now i can only set the `ColorTimer.Interval` to `TimeSpan.FromSeconds` and that sets a specific time... @Enigmativity – luddep Jun 11 '18 at 09:21
  • What do you mean by "not working"? Can you confirm that the timer is firing or not? Is it just that the colour isn't changing? – Enigmativity Jun 11 '18 at 09:25
  • Yes the timer is working! But what i mean is that right now the timers interval is the same for every `Room`. So right now when the `Book` is happening I can only set one value. @Enigmativity – luddep Jun 11 '18 at 09:38
  • What's this about `Room`? There's nothing about rooms in your question. You really need to provide a [mcve] and a proper explanation about what's happening and what you want to happen. Perhaps read [ask] too? – Enigmativity Jun 11 '18 at 09:43
  • But the `Room` isn't very important in this topic. Beacuse the only thing that I need help with is how to make the color change if there is 15 seconds until `timeto` is `DateTime.Now`. @Enigmativity – luddep Jun 11 '18 at 09:56
  • I'm struggling to understand what the issue is. Is it how to calculate the correct `TimeSpan`? Or is it that the colours aren't changing? – Enigmativity Jun 11 '18 at 10:02
  • An [mcve] would let me know what's not working and save a lot of time. – Enigmativity Jun 11 '18 at 10:02
  • It is the calculation of the correct `TimeSpan`. The colors are changing perfectly. Thank you for taking your time for this! @Enigmativity – luddep Jun 11 '18 at 10:11
  • 1
    Is this what you need? `var timeSpan = timeto.Subtract(DateTime.Now).Subtract(TimeSpan.FromSeconds(15.0));` – Enigmativity Jun 11 '18 at 10:26
  • 1
    Ok, I will est that and come back with an answer as soon as possible :) – luddep Jun 11 '18 at 11:18
  • Your solution is working prefectly! :) @Enigmativity – luddep Jun 25 '18 at 14:22

0 Answers0