两个进程之间的时间同步 - .NET [英] Time sync between two processes - .NET

查看:389
本文介绍了两个进程之间的时间同步 - .NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个方案.NET编写的,与采集硬件通过USB进行通信。一台设备是异步的,但它告诉我下到微秒时录得的数据点。另一种是同步的,即,我知道当被记录在每个数据点

I have two programs written in .NET that communicate with acquisition hardware via USB. One device is asynchronous, but it tells me down to the microsecond when a data point was recorded. The other one is synchronous, that is, I know when each data point was recorded.

我有.NET程序使用这些接口。到现在为止,我已经使用DateTime.Now建立在收购开始,然后使用报告的异步装置和同步装置的样本数毫秒/微秒的时间来计算的计算机时间为每个数据点(加入蜱的开始时间)。

I have .NET programs to interface with these. Until now, I have used DateTime.Now to establish when acquisition started, then I use the millisecond/microsecond time reported by the asynchronous device and the sample number from the synchronous device to calculate the computer time for each data point (by adding ticks to the start time).

现在我必须同步时间的两次收购。这两个程序在同一台计算机上运行,​​但我一直在读,DateTime.Now有10到15毫秒根据一些文件的精度(尽管它似乎是几个人能达到基本比这更好的,虽然可以说不仅是因为他们一个Thread.sleep代码后立即调用它)。

Now I must synchronize the times for the two acquisitions. The two programs are running on the same computer, but as I've been reading, DateTime.Now has an accuracy of 10 to 15 milliseconds according to some documentation (although it seems to be that several people can achieve substantially better than this, although arguably only because they call it right after a Thread.Sleep).

所以,据我所知,我目前的方法:

So as far as I know, my current method of:

  1. 在用户开始采集设备上1 DateTime.Now叫
  2. 在用户开始采集设备上的2 DateTime.Now叫

可以给我采集的开始时间一样糟糕,15毫秒关闭。

can give me acquisition start times as bad as 15 milliseconds off.

我能想到的采取DateTime.Now在程序启动,并用秒表记录了程序启动和采集开始之间的确切经过的时间。但是,如果我合并的两个方案一起,将只工作。

I can think of taking a DateTime.Now at program start up, and using StopWatch to record the exact elapsed time between the program start and acquisition start. But that would only work if I merged the two programs together.

任何其他的想法?

推荐答案

秒表具有GetTimeStamp()方法,你可以用做同步。它基本上使用Windows API里面它。你最终得到long类型的蜱数量与StopWatch.Frequency是你的机器每秒滴答数,如果你想转换为秒。

StopWatch has a GetTimeStamp() method that you can use to do the sync. It basically uses the Windows API inside of it. You end up getting a tick number of type long, with StopWatch.Frequency being the number of ticks per second on your machine, if you want to convert to seconds.

这篇关于两个进程之间的时间同步 - .NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆