我正在使用具有C#API的RF阅读器设备。基于它的API,您需要手动调用其读取函数来读取/标记卡。相当于ScheduledExecutorService的C#语言
所以我的解决方法是使用Timer来执行读取每'n'秒。
我的问题是Timer不断执行,不管Thread.sleep()是否在它的动作中被调用。
Timer timer = new Timer(TimerCallback, null, 500, 1000); // From main() method
// The action that Timer executes
private void TimerCallback(Object o)
{
scan(); // Action for reading/badging card
scand.WaitOne(); // AutoResetEvent(true)
GC.Collect(); // Force garbage collection
}
Thread.sleep()在scan()中调用。
在Java中,我使用synchronized()来等待其他线程调用invoke()。我搜索了整整一天,但无法看到等效于ScheduledExecutorService和synchronized()的解决方法。
我希望有一个解决方法,这个原因我需要它尽快。
谢谢!
没有你看看Task类? http://stackoverflow.com/questions/13211334/how-do-i-wait-until-task-is-finished-in-c – fuchs777 2014-09-04 15:22:08
我看到任务类,当我正在搜索,但代码@Erno de Weerd给予解决我的问题。 但无论如何。 – 2014-09-05 12:56:58