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. C#
  4. Time event? Real time data send

Time event? Real time data send

Scheduled Pinned Locked Moved C#
helptutorialquestion
8 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.
  • S Offline
    S Offline
    sodevrom
    wrote on last edited by
    #1

    Hello, I have a project for school and I need it finished a in a few days. What the project does is reads data from a txt file. Each row of the txt file contains a timestamp hh:mm:ss,mmm (example: 00:05:33,141). Whenever the system time is equal to one of the records, I need to send data to a tcp client. What I did is created a thread with a while(true) in it and verified if a row contains the current time, than send the data. That is not good because it's not efficient. I would need something like a timestamp event (not using Timer). Any suggestions/code samples? I would appreciate some help here a lot. Unfortunately I am not good with writing custom events :( So for example I would need an event that will occur on a given time. Example: event(double time,DoThis) . If time is equal to timestamp, than execute function DoThis. In this way I would avoid a while(true) that will eat all processor. Also, System.Threading.Seep is not an alternative because I need this to be precise. Can someone help? Thanks!

    L OriginalGriffO A 4 Replies Last reply
    0
    • S sodevrom

      Hello, I have a project for school and I need it finished a in a few days. What the project does is reads data from a txt file. Each row of the txt file contains a timestamp hh:mm:ss,mmm (example: 00:05:33,141). Whenever the system time is equal to one of the records, I need to send data to a tcp client. What I did is created a thread with a while(true) in it and verified if a row contains the current time, than send the data. That is not good because it's not efficient. I would need something like a timestamp event (not using Timer). Any suggestions/code samples? I would appreciate some help here a lot. Unfortunately I am not good with writing custom events :( So for example I would need an event that will occur on a given time. Example: event(double time,DoThis) . If time is equal to timestamp, than execute function DoThis. In this way I would avoid a while(true) that will eat all processor. Also, System.Threading.Seep is not an alternative because I need this to be precise. Can someone help? Thanks!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      here are a few things for starters: 1. you need to get your jobs sorted chronologically, so if your file isn't, sort it first. 2. Windows timing has limited accuracy. You may want to read Timer surprises, and how to avoid them[^] to get the picture. 3. if you want both good performance and good accuracy (1 second or better), you'll need something clever, probably a combination of some of these: a general timer, a multi-media timer, a stopwatch, a busy loop. 4. if consecutive events are close together, you'll need to take extra care. Now study, and work on your project. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      S 1 Reply Last reply
      0
      • L Luc Pattyn

        here are a few things for starters: 1. you need to get your jobs sorted chronologically, so if your file isn't, sort it first. 2. Windows timing has limited accuracy. You may want to read Timer surprises, and how to avoid them[^] to get the picture. 3. if you want both good performance and good accuracy (1 second or better), you'll need something clever, probably a combination of some of these: a general timer, a multi-media timer, a stopwatch, a busy loop. 4. if consecutive events are close together, you'll need to take extra care. Now study, and work on your project. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        S Offline
        S Offline
        sodevrom
        wrote on last edited by
        #3

        Hello, Thanks for the reply. I don't want to be rude, but it does not help to much. 1. It's sorted chronologically 2. I am already using multimedia timer, and I don't need 1000000 millisecond precission. The time is like : hh:mm:ss,mmm (first 3 digits from milliseconds). 3. This is what I don't know how to do. As I said, I tried in a while(true) loop, and it worked perfectly, but that's not efficient. That's why I was thinking of a custom event. The windows forms timer is similar to what I want, but it's not good. The windows forms timer receives an interval and you call start. Instead of this, I would need something like SetFinalTime(time,WhatToDo). When time equals the system time, than execute the function WhatToDo . That would be my idea, but I don't know how to implement it. Believe me,I did my reading :) 4. They are close, yes, but I will figure that out on the way. First I need point 3 solved ... the event (or any other suggestion). Thanks! Vlad

        L 1 Reply Last reply
        0
        • S sodevrom

          Hello, Thanks for the reply. I don't want to be rude, but it does not help to much. 1. It's sorted chronologically 2. I am already using multimedia timer, and I don't need 1000000 millisecond precission. The time is like : hh:mm:ss,mmm (first 3 digits from milliseconds). 3. This is what I don't know how to do. As I said, I tried in a while(true) loop, and it worked perfectly, but that's not efficient. That's why I was thinking of a custom event. The windows forms timer is similar to what I want, but it's not good. The windows forms timer receives an interval and you call start. Instead of this, I would need something like SetFinalTime(time,WhatToDo). When time equals the system time, than execute the function WhatToDo . That would be my idea, but I don't know how to implement it. Believe me,I did my reading :) 4. They are close, yes, but I will figure that out on the way. First I need point 3 solved ... the event (or any other suggestion). Thanks! Vlad

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          So you have the necessary tools in your tool box. Switch on the brain cells. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          S 1 Reply Last reply
          0
          • L Luc Pattyn

            So you have the necessary tools in your tool box. Switch on the brain cells. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            S Offline
            S Offline
            sodevrom
            wrote on last edited by
            #5

            I really tried, believe me, but I have no ideas how to do it. SImply no ideas. If I had ideas, I wouldn't have posted here :)

            1 Reply Last reply
            0
            • S sodevrom

              Hello, I have a project for school and I need it finished a in a few days. What the project does is reads data from a txt file. Each row of the txt file contains a timestamp hh:mm:ss,mmm (example: 00:05:33,141). Whenever the system time is equal to one of the records, I need to send data to a tcp client. What I did is created a thread with a while(true) in it and verified if a row contains the current time, than send the data. That is not good because it's not efficient. I would need something like a timestamp event (not using Timer). Any suggestions/code samples? I would appreciate some help here a lot. Unfortunately I am not good with writing custom events :( So for example I would need an event that will occur on a given time. Example: event(double time,DoThis) . If time is equal to timestamp, than execute function DoThis. In this way I would avoid a while(true) that will eat all processor. Also, System.Threading.Seep is not an alternative because I need this to be precise. Can someone help? Thanks!

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              If (as you say to Luc) the data is sorted in date time order then why not work out the number of seconds between now and the next message being due, and set up a timer for that interval. Start the timer as a single-shot, and when it fires...

              Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • S sodevrom

                Hello, I have a project for school and I need it finished a in a few days. What the project does is reads data from a txt file. Each row of the txt file contains a timestamp hh:mm:ss,mmm (example: 00:05:33,141). Whenever the system time is equal to one of the records, I need to send data to a tcp client. What I did is created a thread with a while(true) in it and verified if a row contains the current time, than send the data. That is not good because it's not efficient. I would need something like a timestamp event (not using Timer). Any suggestions/code samples? I would appreciate some help here a lot. Unfortunately I am not good with writing custom events :( So for example I would need an event that will occur on a given time. Example: event(double time,DoThis) . If time is equal to timestamp, than execute function DoThis. In this way I would avoid a while(true) that will eat all processor. Also, System.Threading.Seep is not an alternative because I need this to be precise. Can someone help? Thanks!

                A Offline
                A Offline
                Anthony Mushrow
                wrote on last edited by
                #7

                It's true that while running through a loop constantly is not very efficient it is going to be the most accurate for you. What you really need to decide is how accurate you really need this to be. You say that when you match up with one of the times you have you will send data to a TCP client, sending data over the network (unless the destination is actually your own machine) will have a small delay of some milliseconds to actually get sent and then it will be a little longer before it arrives at where it needs to be. That being the case you may be able to set up a standard System.Timers.Timer and use a reasonably small Tick (say 50 milliseconds or something). All you'd need to do then is know the current time and check to see if there are any times in your list that you have gone past since the last time you checked. In this case there would be a maximum delay of 50ms since the time in your list but of course you could lower it for a little more accuracy (Keeping in mind that at the best of times the standard timer will only get called every 20ms) or indeed make the gap larger which would save some of your precious CPU time. There's no way you're going be able to set up any kind of event that will be fired accurately down to the millisecond, even with a while loop and a performance counter it's easily possible that you may skip past a time (and therefore miss the event or fire it 'late'). So like I said you need to decide how much delay you can tolerate if the answer is 'None' then you will have to stick with your while loop because that is as accurate as it's going to get

                My current favourite quote is: Punch them in the face, see what happens!

                -SK Genius

                1 Reply Last reply
                0
                • S sodevrom

                  Hello, I have a project for school and I need it finished a in a few days. What the project does is reads data from a txt file. Each row of the txt file contains a timestamp hh:mm:ss,mmm (example: 00:05:33,141). Whenever the system time is equal to one of the records, I need to send data to a tcp client. What I did is created a thread with a while(true) in it and verified if a row contains the current time, than send the data. That is not good because it's not efficient. I would need something like a timestamp event (not using Timer). Any suggestions/code samples? I would appreciate some help here a lot. Unfortunately I am not good with writing custom events :( So for example I would need an event that will occur on a given time. Example: event(double time,DoThis) . If time is equal to timestamp, than execute function DoThis. In this way I would avoid a while(true) that will eat all processor. Also, System.Threading.Seep is not an alternative because I need this to be precise. Can someone help? Thanks!

                  A Offline
                  A Offline
                  Anthony Mushrow
                  wrote on last edited by
                  #8

                  I think I have a decent solution to your problem which would give the accuracy you want without killing the CPU:

                  class bleh
                  {
                  List times;
                  DateTime next;
                  bool quit = false;

                  void main()
                  {
                  while(!quit)
                  {
                  bool resting = false;
                  //Search through your list and find the next time that will be reached
                  next = ...;

                    do
                    {
                      resting = false;
                      TimeSpan diff = next - DateTime.Now;
                  
                      //If the time difference between now and the next event is greater
                      // than some tolerable value, sleep for a while
                      if(diff > someVal)
                      {
                        resting = true;
                        sleep;
                      }
                    }
                    while(resting);
                  
                    while( true )
                    {
                      if(DateTime.Now >= next)
                      {
                        fire event;
                        break;
                      }
                    }
                  }
                  

                  }
                  }

                  You'd want to be running this in a separate thread so it doesn't interfere with your main application and you'll want to replace DateTime.Now and whatnot with queries to your multimedia timer and you may also have to add some stuff for events that are very close together. Basically if there is a large enough gap in time before the next event you should be able to afford to put the thread to sleep and when you get close to the event you will have to go into the while loop to make sure that the event is fired as close to the specified time as possible. What amount of time you use to switch between sleeping and looping is entirely up to you and I imagine will involve some experimentation. Well, I hope that gives you some idea on what you could do to improve performance, without (hopefully) sacrificing too much accuracy.

                  My current favourite quote is: Punch them in the face, see what happens!

                  -SK Genius

                  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