[.net] Are controls threaded?

Started by
2 comments, last by Amadeus H 12 years, 6 months ago
Hi all, sorry if this is a total nooby question but I've been trying to create a level editor by integrating XNA with winforms much like the winforms example on the XNA site. I've started getting a System.InvalidOperationException and it seems to say that I'm calling my spritebatch.begin again before calling spritebatch.end. I have 4 controls that all call the same static method to draw some UI on each of them. On application.idle I've attached the Invalidate delegate to make each control redraw itself.

The only thing I can think of that's causing these errors is if the controls are threaded and are accessing the function at the same time causing spritebatch.begin to be called a second time before the first is done calling spritebatch.end. They have no problem drawing the 3d stuff, only with the spritebatch. Any ideas?
Advertisement
A clue is in the post you made:

[color=#1C2837][size=2]I'm calling my spritebatch.begin again before calling spritebatch.end[/quote]

WinForms is not multi-threaded, it runs within a single thread, however I don't think that is the problem. From the quote above it seems your code Is trying to do begin one spritebatch before another has finished, in most graphic API's this would cause an error, so in my eyes, this is down to game code and your ordering.

Aimee.

Hi all, sorry if this is a total nooby question but I've been trying to create a level editor by integrating XNA with winforms much like the winforms example on the XNA site. I've started getting a System.InvalidOperationException and it seems to say that I'm calling my spritebatch.begin again before calling spritebatch.end. I have 4 controls that all call the same static method to draw some UI on each of them. On application.idle I've attached the Invalidate delegate to make each control redraw itself.

The only thing I can think of that's causing these errors is if the controls are threaded and are accessing the function at the same time causing spritebatch.begin to be called a second time before the first is done calling spritebatch.end. They have no problem drawing the 3d stuff, only with the spritebatch. Any ideas?

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

I appreciate the reply but this can't be the case as this spritebatch.begin call is the only time I use it, there is no instance of me using spritebatch.begin anywhere else. It is used only in a static method in a static class that 4 different controls use to draw some UI on themselves. Surely therefore these control must be overlapping and trying to access this same static method at the same time, which would mean they would have to be running on separate threads, am I right?

I've put a lock in around this spritebatch call and the error doesn't seem to have appeared again since then so I guess the initial problem is solved but I would still like some more light shed on this if anyone has any answers?
I'm not sure what's causing your particular errors, but in C# controls and their subsequent events run on the UI thread (which is why you need to invoke delegate to change them if you run a seperate thread, like a timer).

This topic is closed to new replies.

Advertisement