Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Please help me to figure out a purpose of this code.

Please help me to figure out a purpose of this code.

Scheduled Pinned Locked Moved WPF
questionwpfdesignregexarchitecture
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mycroft Holmes

    This code would be called in a loop structure. First time it is called _LastScanTime is populated and the ScanTime is irrelevant. All subsequent calls update the ScanTime using subtraction and the _LastScanTime with the current time. I suggest you get a book (or many books) and work through the examples if this simple logic baffles you.

    Never underestimate the power of human stupidity RAH

    P Offline
    P Offline
    Pew_new
    wrote on last edited by
    #3

    @__Holmes__, my dear, oddly enough, I knew a structure of this code, like nobody's business. I asked a bit a different stuff. Let me repeat that: I wish I knew what we really have useful by substraction as follows: ScanTime = DateTime.Now - _lastScanTime?? When the code is running I have a quickly alterating value 250-270 ms. P.S. Next time, before replying, just try to read posts more carefully. As concerned books, please do me a favour, show me a book where is written for what it is being done by mentioned substraction. I'll be waiting for such useful book. Pew

    M 1 Reply Last reply
    0
    • P Pew_new

      @__Holmes__, my dear, oddly enough, I knew a structure of this code, like nobody's business. I asked a bit a different stuff. Let me repeat that: I wish I knew what we really have useful by substraction as follows: ScanTime = DateTime.Now - _lastScanTime?? When the code is running I have a quickly alterating value 250-270 ms. P.S. Next time, before replying, just try to read posts more carefully. As concerned books, please do me a favour, show me a book where is written for what it is being done by mentioned substraction. I'll be waiting for such useful book. Pew

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #4

      So you have a ScanTime of 250-270ms. It is generated by subtracting the current time (DateTime.Now) from the _LastScanTime which was populated by the last loop. It represents the number of milliseconds the system takes to complete the processing loop from the VM. PS It is still a very straight forward piece of code which is staggeringly simple. Try adding

      Thread.Sleep(100)

      after your subtraction line and see what happens.

      Never underestimate the power of human stupidity RAH

      P 1 Reply Last reply
      0
      • P Pew_new

        This is in a Class called ST:

        public Class ST
        {
        private readonly System.Timers.Timer _timer;
        private DateTime _lastScanTime;

        public ST()
        {

                \_timer = new System.Timers.Timer();
                \_timer.Interval = 250;
                \_timer.Start();
                \_timer.Elapsed += OnTimerElapsed;
            }
        
        
        
             public TimeSpan ScanTime { get; private set; }
        
              private void OnTimerElapsed(object sender, ElapsedEventArgs e)
            {
                try
                {
                    \_timer.Stop();
                    ScanTime = DateTime.Now - \_lastScanTime;
                    
                    OnValuesRefreshed();
                }
                finally
                {
                    \_timer.Start();
                }
                \_lastScanTime = DateTime.Now;
            }
            
            private void OnValuesRefreshed()
            {
                ValuesRefreshed?.Invoke(this, new EventArgs());
            }
        

        }

        Then a value of Scantime is being transferred via MVVM pattern (ViewModel) to UI. The major question is what do we really have valuable when we do a substraction as follows: ScanTime = DateTime.Now - _lastScanTime? This point I can't figure out. Thanks in advance!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        Pew_new wrote:

        The major question is what do we really have valuable when we do a substraction as follows: ScanTime = DateTime.Now - _lastScanTime?

        I cannot see anything 'valuable' in this. It merely gives you the actual elapsed time between the timer start and the first tick. Since your timer interval is set at 250 (milliseconds) it should be somewhere around that value. The question really is what this code is being used for?

        P 1 Reply Last reply
        0
        • M Mycroft Holmes

          So you have a ScanTime of 250-270ms. It is generated by subtracting the current time (DateTime.Now) from the _LastScanTime which was populated by the last loop. It represents the number of milliseconds the system takes to complete the processing loop from the VM. PS It is still a very straight forward piece of code which is staggeringly simple. Try adding

          Thread.Sleep(100)

          after your subtraction line and see what happens.

          Never underestimate the power of human stupidity RAH

          P Offline
          P Offline
          Pew_new
          wrote on last edited by
          #6

          Holmes> It represents the number of milliseconds the system takes to complete the processing loop from the VM. I was waiting for this sentence.:thumbsup: For what this stands for? I mean, e.g. a label with mentioned milliseconds inside, on UI screen. Just show to UI user that app is alive?

          1 Reply Last reply
          0
          • L Lost User

            Pew_new wrote:

            The major question is what do we really have valuable when we do a substraction as follows: ScanTime = DateTime.Now - _lastScanTime?

            I cannot see anything 'valuable' in this. It merely gives you the actual elapsed time between the timer start and the first tick. Since your timer interval is set at 250 (milliseconds) it should be somewhere around that value. The question really is what this code is being used for?

            P Offline
            P Offline
            Pew_new
            wrote on last edited by
            #7

            Richard MacCutchan> The question really is what this code is being used for? I would ask you this way. :) My guess is it provides an UI user some kind of "Warning". Something like "Your PC's processor is so busy that the ScanTime went out of allowed time range." Of course in case when value is out of tolerance "window". A value is out of tolerancee "window" - text of value is highlighted with red.

            L 1 Reply Last reply
            0
            • P Pew_new

              Richard MacCutchan> The question really is what this code is being used for? I would ask you this way. :) My guess is it provides an UI user some kind of "Warning". Something like "Your PC's processor is so busy that the ScanTime went out of allowed time range." Of course in case when value is out of tolerance "window". A value is out of tolerancee "window" - text of value is highlighted with red.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              So why exactly are you asking here? We have no information on the context that this code runs inside, or what the application is trying to do.

              P 1 Reply Last reply
              0
              • L Lost User

                So why exactly are you asking here? We have no information on the context that this code runs inside, or what the application is trying to do.

                P Offline
                P Offline
                Pew_new
                wrote on last edited by
                #9

                Some info I gave in the first post: Timer -> Timer procedure and calculation -> MVVM pattern -> UI. As said Mr.Holmes. It's very simple code.:thumbsup:

                L 1 Reply Last reply
                0
                • P Pew_new

                  Some info I gave in the first post: Timer -> Timer procedure and calculation -> MVVM pattern -> UI. As said Mr.Holmes. It's very simple code.:thumbsup:

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  Which means absolutely nothing. I still do not understand exactly what you are asking.

                  P 1 Reply Last reply
                  0
                  • L Lost User

                    Which means absolutely nothing. I still do not understand exactly what you are asking.

                    P Offline
                    P Offline
                    Pew_new
                    wrote on last edited by
                    #11

                    It may close this topic. I've got comrehensive answer from Mr.Holmes. If you're interested in. This is answer: It represents the number of milliseconds the system takes to complete the processing loop from the VM.

                    L 1 Reply Last reply
                    0
                    • P Pew_new

                      It may close this topic. I've got comrehensive answer from Mr.Holmes. If you're interested in. This is answer: It represents the number of milliseconds the system takes to complete the processing loop from the VM.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      No, it is just the number of milliseconds between the timer starting, and the Elapsed event being fired. This will occur after approximately 250 milliseconds as declared by the timer intialisation values.

                      M 1 Reply Last reply
                      0
                      • P Pew_new

                        This is in a Class called ST:

                        public Class ST
                        {
                        private readonly System.Timers.Timer _timer;
                        private DateTime _lastScanTime;

                        public ST()
                        {

                                \_timer = new System.Timers.Timer();
                                \_timer.Interval = 250;
                                \_timer.Start();
                                \_timer.Elapsed += OnTimerElapsed;
                            }
                        
                        
                        
                             public TimeSpan ScanTime { get; private set; }
                        
                              private void OnTimerElapsed(object sender, ElapsedEventArgs e)
                            {
                                try
                                {
                                    \_timer.Stop();
                                    ScanTime = DateTime.Now - \_lastScanTime;
                                    
                                    OnValuesRefreshed();
                                }
                                finally
                                {
                                    \_timer.Start();
                                }
                                \_lastScanTime = DateTime.Now;
                            }
                            
                            private void OnValuesRefreshed()
                            {
                                ValuesRefreshed?.Invoke(this, new EventArgs());
                            }
                        

                        }

                        Then a value of Scantime is being transferred via MVVM pattern (ViewModel) to UI. The major question is what do we really have valuable when we do a substraction as follows: ScanTime = DateTime.Now - _lastScanTime? This point I can't figure out. Thanks in advance!

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #13

                        There appear to be a couple of structural issues with this code. The first thing is, you haven't shown us the complete class. Secondly, you update _lastScanTime to a different DateTime.Now than when you update ScanTime - in a multi-threaded application, this could be a potentially significant difference because of your ValuesRefreshed. To be honest, I don't know why you don't make ScanTime raise INotifyPropertyChanged.PropertyChanged and avoid other events; you can bind directly to that value. Also, I would use a StopWatch instead of DateTime.Now if I were you.

                        This space for rent

                        1 Reply Last reply
                        0
                        • L Lost User

                          No, it is just the number of milliseconds between the timer starting, and the Elapsed event being fired. This will occur after approximately 250 milliseconds as declared by the timer intialisation values.

                          M Offline
                          M Offline
                          Mycroft Holmes
                          wrote on last edited by
                          #14

                          Richard MacCutchan wrote:

                          This will occur after approximately 250 milliseconds as declared by the timer intialisation values.

                          Ah crap I completely missed that, even after rereading the dammed code.

                          Never underestimate the power of human stupidity RAH

                          L 1 Reply Last reply
                          0
                          • M Mycroft Holmes

                            Richard MacCutchan wrote:

                            This will occur after approximately 250 milliseconds as declared by the timer intialisation values.

                            Ah crap I completely missed that, even after rereading the dammed code.

                            Never underestimate the power of human stupidity RAH

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #15

                            We've all done that from time to time. Although, I was actually worried that I was missing something that you had noticed.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups