2

I am working on a class library that tracks transit vehicles in close to real time using GPS lat/long co-ordinates.

The library gets the vehicles positions from a rest XML web service provided by the transit service. I want the vehicle coordinates to automatically update them selves without having to manually call and update them through a refresh method in my application. The class should automatically update itself with the latest data and raise an event when it updates.

Is it possible to auto refresh the data on a timer inside of an object using a timed event or is this not possible with out running a message pump within the class

I apologize if there is an existing question for this type of thing but if there is I couldn't find it using Google or the site search.

Chris McGrath
  • 1,727
  • 3
  • 19
  • 45

1 Answers1

2

You can use a System.Threading.Timer. It doesn't need a message loop as it "Ticks" on thread pool threads. That does mean you'll have to marshal back to the UI thread to update the UI.

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • so i can use an event handler in an object without needing pumps . so i can theoretically then also raise an event that my ui thread could use to update my WPF controls with the new information then ? – Chris McGrath Apr 16 '12 at 23:52
  • Yes, although as I said, the "Tick" method is called on a thread pool thread. So if you just raise an event in that method, the subscriber methods will also be run on the thread pool thread. Somewhere between the "Tick" method and updating UI controls, you will have to marshal back to the UI thread. – Nick Butler Apr 17 '12 at 08:12
  • im a bit new to multi-threading what exactly is marshalling and would i go about that – Chris McGrath Apr 17 '12 at 23:16
  • There's an excellent free intro ebook here: [Albahari](http://www.albahari.com/threading/) – Nick Butler Apr 18 '12 at 08:04