-2

I'm trying to write a game loop in C#, but I can't access the WPF objects from another thread...

Basically this is what I want in my loop:

Loop:

Read Input Update game state Render to a buffer Send buffer to graphic card show rendered image on screen repeat.

What is the best way to do this? I could do this with Dispatcher.Invoke, but I don't think Dispatcher.Invoke is made for normal UI stuff.

NomenNescio
  • 2,899
  • 8
  • 44
  • 82
  • My question is, why are you writing a game in wpf? If it's a simple low fidelity graphics affair then fair enough but there are frameworks such as XNA which provide services such as basic game loops, content pipeline, rendering services and more already out there. Another good game dev platform is Unity – Charleh Mar 15 '14 at 17:20
  • @Charleh I want to write a RayTracer in C#, all I need is setpixel(x, y, color), XNA seems a bit overkill. I want a simple, minimalistic loop. – NomenNescio Mar 18 '14 at 21:55

1 Answers1

2

I could do this with Dispatcher.Invoke, but I don't think Dispatcher.Invoke is made for normal UI stuff.

Sarcastic question - what else do you think it is made for? It is made exactly for that - so that a non UI Thread can invoke back to the UI thread to update the UI. That is exactly the ONLY use to EVER call it - if I do not invoke to the UI thread, why the heck should I pay the runtime cost of doing that?

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • Yes I understand, but you have absolutely no control over how long it will take, with a game loop there should be an expectation that it each frame takes around 20-30 milliseconds, is Dispatcher.Invoke reliable for this sort of thing? – NomenNescio Mar 17 '14 at 07:25
  • @ProgrammerAtWork Depends how bad you program. You should invoke ONCE into the UI thread for all updates of a loop. And no, you never have guarantees - not in WPF. But WPF is quite fast. Again, invoke into the ui thread ONCE per loop and then - use a profiler in WPF to see where WPF spends time (and yes, this stuff is free, if you know how to use google - MS has some nice profilers that show you where exactly WPF spends time). – TomTom Mar 17 '14 at 07:31