C# How to make the time go backwards?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2008 Views - Last Post: 03 October 2017 - 05:13 AM Rate Topic: -----

#16 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 03:21 PM

Adjusted code for MVVM. And I agree with what everyone has said here as well as your code will be much more robust.

StartTimer = new DelegateCommand(OnExecuteCountDown);

public void OnExecuteCountDown(object args)
{
    DispatcherTimer countTimer = null;
    TimeSpan countDown;
    countDown = TimeSpan.FromSeconds(60);

    countTimer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
    {
        TimerChangedText = countDown.ToString();
        if (countDown == TimeSpan.Zero) countTimer.Stop();
        countDown = countDown.Add(TimeSpan.FromSeconds(-1));
    }, Application.Current.Dispatcher);

    countTimer.Start();
}


private string _timerChangedText;

public string TimerChangedText
{
   get { return _timerChangedText; }
   set
   {
       _timerChangedText = value;
       onpropertychanged();
   }
}



And XAML
<Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="35"/>
      <RowDefinition Height="35"/>
    </Grid.RowDefinitions>
        <Button Command="{Binding StartTimer}" Grid.Row="0"></Button>
        <TextBlock Text="{Binding TimerChangedText}" Grid.Row="1"></TextBlock>
</Grid>

This post has been edited by JapanDave: 02 October 2017 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#17 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 04:15 PM

View PostSkydiver, on 02 October 2017 - 06:46 AM, said:

+100.

Anyway, back on topic regarding the problem. This is a really simple issue of adding or subtracting time from a TimeSpan.

Rough pseudo code:
class CountdownStopwatch
{
     TimeSpan TimeLeft = { get; private set; }
     DateTime StartTime { get; private set; }

     public void CountdownStopwatch(TimeSpan timeLeft)
     {
         TimeLeft = timeLeft;
     }

     public void Start()
     {
         StartTime = DateTime.Now;
     }

     public void Stop()
     {
         TimeLeft -= DateTime.Now - StartTime;
     }
}




I just realized another issue, to make it run in the background, so I press the button and the timer runs, I can't get it to run, so I need a async method or inherit the service class?
Was This Post Helpful? 0
  • +
  • -

#18 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5886
  • View blog
  • Posts: 20,092
  • Joined: 05-May 12

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 08:34 PM

How do you make it run counting up? It should work exactly the same way counting back down. The only difference is that instead of directly printing out the the elapsed time of the Stopwatch class, you would be printing out the elapsed time of the CountdownStopwatch.

Anyway, all of this would be moot if you were using WPF and the MVVM pattern. You could simply fire off a task or thread to periodically increase/decrease the value within your model. The view model would send notifications to the view that the value has changed, and so the WPF data binding would get the notifications and update the UI.
Was This Post Helpful? 0
  • +
  • -

#19 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 09:36 PM

Ah ok thanks

View PosttlhIn`toq, on 02 October 2017 - 05:27 AM, said:

View PostDialupp, on 02 October 2017 - 04:12 AM, said:

and recently did 2 c# courses to fill some holes in my learning it was intermediate and advanced stuff.


I'm sorry to break it to you... but you don't have basics down - certainly not for 21st century design and cross-platform development. Most of that wouldn't have been acceptable in WPF in 2012. Moving on to intermediate and advanced... well... You don't start building the 2nd story on top of a shaking first story, on top of an unstable foundation.

Just because you're frustrated with, and don't grasp the courses you went through doesn't mean you can just skip them or they weren't important. You need those foundation principles. They are the basis of your app and what you build on top of.

If the construction metaphors aren't clicking how about navigation?
On a 10,000 mile trip you can be off by 1 degree in the last mile and still see your destination.
But if you start off 1 degree off course and continue for the duration of all 10,000 miles... that slight error will have you missing your destination by an entire continent.

I'm not saying this to be mean or tear you down. I'm saying this because I want to see you succeed. I don't want to see you invest 5,000 hours along this direction and have to post

Quote

"Well I did xyz and now I've painted myself into a corner. How can I fix all this without having to throw out my last 6 months work?"
Its a lot better to spend a couple hundred hours on education than to waste a couple thousand on work that has to be tossed.



Well what concepts am I missing mvvm? Is this the basics you are talking about? Also how long does it take to master the basics ffs.

Also should I put my project on pause and do the Xamarin university you speak of?
Was This Post Helpful? 0
  • +
  • -

#20 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,372
  • Joined: 02-June 10

Re: C# How to make the time go backwards?

Posted 03 October 2017 - 04:20 AM

Here's an async version that doesn't use a timer. Its less process intensive.
This is not tailored for your app. Its is copy/paste right out of one of my apps and this is more MVVM that what you've got going.
It raises a 'Heartbeat' every one second, and updates the app's DeviceDateTime property (meaning you can bind a label to it on your UI so you have an on-screen clock) as well as raise an event so you can react to the clock going "tick". Every 60 heartbeats you could do something that you want to re-occur every one minute for example.

        #region Real-time clock event and property 
        async void Heartbeat()
        {
            while (true)
            {
                await Task.Delay(1000);
                DeviceDateTime = DateTime.Now;
            }
        }

        #region DeviceDateTime (DateTime)
        private DateTime _DeviceDateTime;
        public DateTime DeviceDateTime
        {
            get
            {
                return _DeviceDateTime;
            }

            set
            {
                if (_DeviceDateTime == value) return;
                _DeviceDateTime = value;
                onpropertychanged();
                RaiseClockTick();
            }
        }
        #endregion DeviceDateTime  (DateTime)

        /// <summary>App-scoped 'heartbeat' to keep everyone in sync
        /// 
        /// </summary>
        public event EventHandler<DateTime> ClockTick;
        protected virtual void RaiseClockTick()
        {
            ClockTick?.Invoke(this, DeviceDateTime);
        }


        #endregion Real-time clock event and property




Was This Post Helpful? 0
  • +
  • -

#21 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,372
  • Joined: 02-June 10

Re: C# How to make the time go backwards?

Posted 03 October 2017 - 04:30 AM

View PostDialupp, on 02 October 2017 - 11:36 PM, said:

Well what concepts am I missing mvvm? Is this the basics you are talking about? Also how long does it take to master the basics ffs.

Also should I put my project on pause and do the Xamarin university you speak of?


> Well what concepts am I missing mvvm?
The code that you posted at the start of this thread is 100% devoid of MVVM design. So I can only assume you are missing all of that concept. Along with that comes things like how to separate UI from logic: Your logic is very tightly coupled with the UI and that's a bad thing.

> Also should I put my project on pause and do the Xamarin university you speak of?
Yes. I've said it many times in your threads and also in our PM chat.

> Also how long does it take to master the basics ffs.
How long does it take to learn anything? How long to learn how to blow glass into a vase? It all depends on the person. But... Based on what I've seen of your coding style you're coding as if it were still WinForms in 2003. You've got about 15 years of coding style and concepts to catch up on. How long that takes you is something only you can figure out. Do yo have 8 hours a day to spend on it? Or 8 hours a week? How many times do you have to go over the material before you know it - because you've said in this thread that you've done several of the tutorials over and over and still don't grasp them. There is no way anyone else can tell you it will take 7,438 hours to learn the material. But the general number people throw around is 5,000 hours to learn the basics. Those students tend to be the 'interns' that JUST know the basics and really couldn't architect a new piece of software from scratch. 5,000 hours is roughly enough to get you to the point of being a beginning code monkey that someone would hire for grunt coding. From there you would learn on the job. Another couple years to promote to Software Engineer II. Another couple years to Senior Software Engineer, which is where you've probably learned enough on the job, and seen enough false starts on programs that you could do a pretty decent job of architecting a brand new program without falling into the traps or painting yourself into a corner.
Was This Post Helpful? 1
  • +
  • -

#22 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: C# How to make the time go backwards?

Posted 03 October 2017 - 04:53 AM

Ok, I'll do what you said from tomorrow I'll try the xamarin university, if it doesn't go well I will just probably buy a 15 dollar mvvm course on udemy.
Was This Post Helpful? 0
  • +
  • -

#23 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: C# How to make the time go backwards?

Posted 03 October 2017 - 05:13 AM

View PostDialupp, on 02 October 2017 - 09:36 PM, said:

Well what concepts am I missing mvvm? Is this the basics you are talking about? Also how long does it take to master the basics ffs.

Also should I put my project on pause and do the Xamarin university you speak of?


I started C# completely from scratch a little over a year ago and I am completely self taught and apart from a few push and shoves from various people here and StackOveflow, this is one of my first posts here , One of my first questions here. I started with WinForms for the first few weeks until I was told in that thread that I should be doing WPF and I finally started MVVM about 6 months ago.

I just completed a relatively big application that deals with Staff Management and Workforce Scheduling and I currently migrating the app to work via web browser and mobile platforms.

MVVM is a bit of a learning curve, but time and practice is all you have.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2