我正在开发一个DLL,它将为在同一台机器上运行的多个应用程序提供同步时间戳。在使用高性能计时器和标量的线程中更改时间戳,以提供比实时更快的移动外观。出于显而易见的原因,我只想要这个时间库的一个实例,我想我可以使用WCF连接到其他进程并随时轮询时间戳。当我连接时,我从来没有得到有效的时间戳,只是一个空的DateTime。我应该指出图书馆确实有效。原始实现是一个单独的DLL,每个应用程序合并,每个应用程序使用Windows消息同步。我很确定它与我如何设置WCF的东西有关,我仍然很新。以下是合同定义:
public interface ITimerCallbacks { [OperationContract(IsOneWay = true)] void TimerElapsed(String id); } [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ITimerCallbacks))] public interface ISimTime { [OperationContract] DateTime GetTime(); }
这是我的班级定义:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class SimTimeServer: ISimTime
主机设置:
// set up WCF interprocess comms host = new ServiceHost(typeof(SimTimeServer), new Uri[] { new Uri("net.pipe://localhost") }); host.AddServiceEndpoint(typeof(ISimTime), new NetNamedPipeBinding(), "SimTime"); host.Open();
和服务器端接口函数的实现:
public DateTime GetTime() { if (ThreadMutex.WaitOne(20)) { RetTime = CurrentTime; ThreadMutex.ReleaseMutex(); } return RetTime; }
最后是客户端实现:
Callbacks myCallbacks = new Callbacks(); DuplexChannelFactory pipeFactory = new DuplexChannelFactory(myCallbacks, new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/SimTime")); ISimTime pipeProxy = pipeFactory.CreateChannel(); while (true) { string str = Console.ReadLine(); if (str.ToLower().Contains("get")) Console.WriteLine(pipeProxy.GetTime().ToString()); else if (str.ToLower().Contains("exit")) break; }
答案 0 :(得分:0)
我甚至没有达到需要回调的程度。我计划模仿Windows计时器功能,以提供可以使用加速时间戳的计时器。这就是回调将被使用的地方。
GetTime方法是获取当前时间的方法(将其想象为DateTime.Now方法以获取当前系统时间)。