0

I need to read data from reader continuously and send it to the caller. I created self hosted window service to act as a signalR. I started infinite loop inside it.

public void ReadTags()
{
    try
    {
        while (true)
        {
            var store = Reader.Read();
            var tags = store.ToArray();
            Clients.Caller.getTagsRead(tags);
            System.Threading.Thread.Sleep(500);
        }
    }
    catch (Exception ex)
    {
            System.Diagnostics.EventLog.WriteEntry("Send", ex.Message + "\n" + ex.StackTrace);
    }
}

Now, I want to have a control on this method to be able to pause, play or stop this loop next time when a method is invoked. Since Hub creates an instance everytime, this means loop will be running under a different instance.

How can I achieve this?

Joel Bourbonnais
  • 2,618
  • 3
  • 27
  • 44
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286

1 Answers1

0

I think you should run this loop within a Task in a separate helper. This helper could be made Singleton through an IoC. Your hubs could then receive the helper by Dependency Injection. This way, you would have control on the Task instance.

To integrate IoC with SignalR, I liked using LightInject.

Finally, to call clients from outside the hubs.

Community
  • 1
  • 1
Joel Bourbonnais
  • 2,618
  • 3
  • 27
  • 44