Sign in to follow this  
EvilWeebl

[.net] Are controls threaded?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
A clue is in the post you made:

[quote][color=#1C2837][size=2]I'm calling my spritebatch.begin again before calling spritebatch.end[/size][/color][/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.
[quote name='EvilWeebl' timestamp='1317393222' post='4867584']
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?
[/quote]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this