1

I have this sample C# code that connects to the EyeX engine of the Tobii EyeX eye tracking device, subscribes to a gaze data stream and writes the result to the console.

var eyeXHost = new EyeXHost();
var stream = eyeXHost.CreateGazePointDataStream(GazePointDataMode.LightlyFiltered);
eyeXHost.Start();
stream.Next += (s, e) => Console.WriteLine("Gaze point at ({0:0.0}, {1:0.0}) @{2:0}", e.X, e.Y, e.Timestamp);
Console.In.Read();

I'm calling this code from a Node.js script with the help of Edge.js. I was able to get it to work but I found that I had to modify the code in ways that I don't fully understand.

var eyeXHost = new EyeXHost();
var stream = eyeXHost.CreateGazePointDataStream(GazePointDataMode.LightlyFiltered);
await Task.Run(() => eyeXHost.Start());
stream.Next += async (s, e) => {
  await onGazePoint(new GazePoint(e.X, e.Y, e.Timestamp));
};

Namely, I had to wrap eyeXHost.Start() in an await Task.Run() call. Otherwise, the thread seems to block and no gaze data is received. I also had to add async await to the stream event handler.

Why was it necessary to do this?

melhosseiny
  • 9,992
  • 6
  • 31
  • 48
  • Is your console app immediately exiting after hooking up the `Next` event? – Rob May 10 '17 at 06:35
  • @Rob There's a `Console.In.Read()` after, added to code sample – melhosseiny May 10 '17 at 06:37
  • 1
    One thing that `Task.Run` does is prevent any current `SynchronizationContext` from being picked up. Some components (e.g., [EAP](https://msdn.microsoft.com/en-us/library/ms228969(v=vs.110).aspx)) will capture the current `SynchronizationContext` when their operations start and use it to raise their events. – Stephen Cleary May 11 '17 at 21:22

0 Answers0