0

I'm new to c#.

The problem:

I'd like to run a method within a timer, but the method returns/needs arguments not in the timers set of arguments.

The reason: The method is called regularly (EPOC Emotiv headset) and I would like it to be called only once a second.

It is called (I think) by a function:

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

the method that runs (too regularly) is:

void Instance_CognitivEmoStateUpdated(object sender, EmoStateUpdatedEventArgs e) { EmoState es = e.emoState; EdkDll.EE_CognitivAction_t currentAction = es.CognitivGetCurrentAction(); }

The sotware already comes with a timer runs to process events every second:

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

I wish I could simply place the method aboce (Instance_Cogn...) in the timer, an I think that would sove the problem..

What is the best way to do this please?

Many thx.

  • As per your post you have created an event "CognitivEmoStateUpdated" and when this event happens you are calling Instance_CognitivEmoStateUpdated function. You want to call Instance_CognitivEmoStateUpdated once in a second, so what is the mean of binding this function to event. – Romil Kumar Jain May 15 '12 at 07:06

1 Answers1

1

Use System.Threading.Timer instead of Timer Control. Time class of threading gives you the facility to pass the arguments and use the output of function in code.

   // 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

Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
  • Unfortunately I get a CS0123 Error when I use: TimerCallback timeCB = new TimerCallback(Instance_CognitivEmoStateUpdated); although the method void Instance_CognitivEmoStateUpdated(object sender, EmoStateUpdatedEventArgs e) { } appears there. Do I need to specifiy a library? – Emotiv User May 14 '12 at 17:38
  • share your code in post. How many args you want to pass to timer. – Romil Kumar Jain May 14 '12 at 18:07
  • namespace 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) { code to run every 1 second} – Emotiv User May 14 '12 at 18:13
  • the timer only needs to run the method, I'm not sure I need to send any arguments to it. Thank you :) – Emotiv User May 14 '12 at 18:16