Psuedo code for no code.
if (readline.substring(get the time stamp).equals(timestamp))
DoSomthing();
finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
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
}
}
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
PS: Finito, instead of posting irelevant messages, it's better you not post at all.
Thanks I will take that into consideration
finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
>>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.
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735