Search code examples
winformsmultithreadingmvp

WinForm MVP with Unsolicited Event Thread


I have inherited a large WinForm application that could use some rewriting. I have had success with MVP architecture in ASP.NET Web Forms and thought that would fit well with this application.

The application essentially listens for unsolicited events from an IVR system (phone ringing, call hang-up, etc.) and allows the users to make solicited events to the IVR (dial, transfer, hold, etc.). I have a pretty good grasp of using MVP for the solicited events, but I am hitting a road block conceptualizing how the unsolicited events will be handled.

Today, the application fires up a thread that poles (which is probably more solicited than unsolicited now that I write this) the IVR every 500ms.

Where would this thread fire up? Would it be in the Main form's presenter?

Edit: After a little more thought, I think it would make sense to put it in the Main form's presenter Init method. Am I thinking of this correctly?


Solution

  • I'd put it in the bootstrapper as this seems a to be a fundamental component for your application (if you have one, if not in the same place where the main application form is started).

    I think you should encapsulate this polling logic into some kind of singleton that when determines there is something worth notifying will raise an event.

    One way of achieving this could be creating a singleton with a Timer. In the ontimer event you would go and check the IVR to see if there is anything interesting to notify or not.

    To know who you need to notify you would need either to keep a list of subscribers for those interested in receiving notifications (as in the observer pattern) or have an .net event where your presenters attach/dettach, or you could also use some implementation of detached events as the one that the Prism framework provides.