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.

Can someone help?

Thanks!

Psuedo code for no code.

if (readline.substring(get the time stamp).equals(timestamp))
  DoSomthing();

When comparing times it is best to use <= or >= as it is unlikely that the current time will exactly match the time from your input file.

If you are not happy using a seperate thread then using a System.Timers.Timer is the only other way I can think of.
(Note: This is not the same as System.Windows.Forms.Timer .)

The System.Timer allows you to pass your own object to the executing code when the event is called. This could be the data to send out on TCP.

If the data in the text file is known to be sorted and the interval between events is not too small, then each execution of the timer event code could setup the next timer event.

Here is an example of how to use the System.Timers.Timer

public class TimerExample
{
    System.Threading.Timer timer;

    public void StartTimer()
    {
        string data = "Example Data";
        // create the timer passing the data to use in the timer done event
        timer = new System.Threading.Timer(new System.Threading.TimerCallback(this.TimerDone), data, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
        timer.Change(5000, System.Threading.Timeout.Infinite); //<-- start the timer, for 5000ms
    }

    private void TimerDone(object state)
    {
        string data = state as string;
        // Do something using data string
    }
}

Hello Nick,
Thanks for the advice. ACtually in the end I am going to use the Multimedia Timer (it's the most precise).

I am going to calculate the difference between the timestamp of the next record and the current time, and start the time with that difference.

When the timer event is raised, I am going to send the data.

Thanks!

PS: Finito, instead of posting irelevant messages, it's better you not post at all.

PS: Finito, instead of posting irelevant messages, it's better you not post at all.

Thanks I will take that into consideration

>>ACtually in the end I am going to use the Multimedia Timer (it's the most precise).

I have always been under the impression that Stopwatch was the most accurate .NET timer, but it seems you're right. Thanks for posting that.

Anyway I wanted to mention if you're writing an application extremely sensitive to time you may also consider setting an elevated process priority and thread affinity. This affects how the windows scheduler allocated CPU time to your process:

using (System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess())
      {
        p.PriorityClass = System.Diagnostics.ProcessPriorityClass.RealTime;
      }
      System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;

You should be aware of the implications when using a RealTime process. The OS does not have to honor it, and if something causes your CPU to spin on that process priority you will probably have no choice other than to hard reset your computer.

Thanks a lot for the note!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.