【问题标题】:Placing a method in a timer to slow it down for Emotiv Epoc在计时器中放置一个方法以减慢 Emotiv Epoc 的速度
【发布时间】:2012-05-22 02:15:36
【问题描述】:

我是 C# 新手。

问题

我想在计时器中运行一个方法,但该方法返回/需要的参数不在计时器参数集中。

原因:该方法被定期调用(EPOC Emotiv 耳机),我希望它每秒只调用一次。

它被一个函数调用(我认为):

EmoEngine.Instance.CognitivEmoStateUpdated += new EmoEngine.CognitivEmoStateUpdatedEventHandler(Instance_CognitivEmoStateUpdated);

运行的方法(过于规律)是:

void Instance_CognitivEmoStateUpdated(对象发送者,EmoStateUpdatedEventArgs e) { EmoState es = e.emoState; EdkDll.EE_CognitivAction_t currentAction = es.CognitivGetCurrentAction(); }

该软件已经带有一个计时器运行以每秒处理事件:

private void timer1_Tick(object sender, EventArgs e) { engine.ProcessEvents(); }

我希望我可以简单地将方法 aboce (Instance_Cogn...) 放在计时器中,我认为这样可以解决问题..

请问最好的方法是什么?

非常感谢。

【问题讨论】:

  • 根据您的帖子,您创建了一个事件“CognitivEmoStateUpdated”,当此事件发生时,您正在调用 Instance_CognitivEmoStateUpdated 函数。您想每秒调用一次 Instance_CognitivEmoStateUpdated,那么将此函数绑定到事件是什么意思。

标签: c# timer pooling


【解决方案1】:

使用 System.Threading.Timer 而不是 Timer Control。 线程时间类使您可以传递参数并在代码中使用函数的输出。

   // Create the delegate that invokes methods for the timer.
   TimerCallback timerDelegate = new TimerCallback(CheckStatus);

   // Create a timer that waits one second, then invokes every second.
   Timer timer = new Timer(timerDelegate, arguments,1000, 1000);

Refer the sample code

【讨论】:

  • 感谢您的快速评论。
  • 不幸的是,我在使用时收到 CS0123 错误:TimerCallback timeCB = new TimerCallback(Instance_CognitivEmoStateUpdated);虽然方法 void Instance_CognitivEmoStateUpdated(object sender, EmoStateUpdatedEventArgs e) { } 出现在那里。我需要指定一个库吗?
  • 在帖子中分享您的代码。你想传递多少个参数给计时器。
  • 命名空间 LoadUserProfile {public partial class Cognate : Form { EmoEngine engine = EmoEngine.Instance; public Cognate() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) {EmoEngine.Instance.EmoStateUpdated += new EmoEngine.EmoStateUpdatedEventHandler(Instance_EmoStateUpdated); EmoEngine.Instance.CognitivEmoStateUpdated += new EmoEngine.CognitivEmoStateUpdatedEventHandler(Instance_CognitivEmoStateUpdated); engine.Connect();} void Instance_CognitivEmoStateUpdated(object sender, EmoStateUpdatedEventArgs e) { 每 1 秒运行一次的代码}
  • 计时器只需要运行该方法,我不确定是否需要向它发送任何参数。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 2022-01-20
  • 2011-06-08
  • 2014-09-20
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
相关资源
最近更新 更多