3

I want to create a level editor, i've started it on XNA but finally it would be better using a WindowsFormApplication.

Here is the layout that i want to achieve:

BLUE -> Windows form control

PURPLE -> Canvas, handled differently (Yeah, Windows forms controls too, but not for the same use of it)

I have a problem though, how to render the Canvas in a proper way without using a thread ?

(Look at my other thread here in stackoverflow: https://stackoverflow.com/questions/29463393/access-object-from-other-thread?noredirect=1)

I thought about a timer, but it seemed to be a bad idea for me (Refreshrate).

Ideally a thread would be nice but Windows doesn't like the fact that you want to interact between thread so it's messed up to be polite.

If you know how to achieve that, well, thanks ! :)

Riptide
  • 292
  • 1
  • 2
  • 10

1 Answers1

3

You don't want a thread for this. It doesn't really make sense to get another thread to do timing for you, when you could just use your existing thread for that.

There's a good blog post on the available techniques here. Note the use of the XNA WinForms sample.

If you want the ticking to behave just like the real XNA, use the third method. The gist of it is: Handle the Application.Idle method, and sit in a loop ticking, while using win32's PeakMessage (via P/Invoke) to exit your loop whenever Windows needs you to respond to some other message. (Once WinForms has responded, it will call Idle again to re-enter your loop.)

Note that, if you have more than one GraphicsDeviceControl, you need to ensure there's only one handler for Application.Idle, which then dispatches ticks to all controls.

Finally, to play nice with the system, and to be more like XNA, check whether your application is active (ie: the foreground window). If it is inactive, call Thread.Sleep(20) in your idle loop.

Glorfindel
  • 986
  • 1
  • 9
  • 16
Andrew Russell
  • 21,268
  • 7
  • 56
  • 103