2
votes

I have an XNA application that is never calling Draw() (and update() never gets called once). It is multithreaded. In particular, it holds a collection of objects which each call their own draw function.

The draw function looks like this:

    spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

    foreach (Interaction<InteractionState> interaction in interactions){
        interaction.display(spriteBatch); //this calls the SpriteBatch Draw function
    }

    spriteBatch.End();

Interactions are objects which are updated by a custom input device, which is running on a separate thread.

When I only have one or two of these interactions, things run fairly fast, but when I start putting 3 or more, the drawing stops. Since it works with two Interactions, I am convinced the problem is not a matter of something like redundant content load calls or a bug in calling spritebatch.begin/end twice.

My best guess is that right now, increasing my input sources has increased the computational load on that thread to the point where it never gets a chance to call Draw. How can I force the game to Draw and take precedence over that other thread? I have tried setting the rate

this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 10.0f);

and no inactivity:

this.InactiveSleepTime = TimeSpan.Zero;

Both in Initialize(), and neither has done the trick.

Edit: People seem to be confused (I probably didn't word it well), but, I am ONLY trying to draw in the main thread. I am NOT trying to do any drawing in the background thread. The main draw method is never being called.

Edit 2: I can also say that if I make my background thread sleep for 150ms at a time, then the system starts drawing. That's too low of a framerate for my purposes though.

1
I'm pretty sure drawing can't be done with multiple threads.Jashaszun
What do you mean by that? Do you mean that drawing can't be done in two separate threads? I note that I am NOT trying to draw in multiple threads, I am only drawing in one thread, with a background thread running. Is there a reason why a background thread would interfere with Drawing?user650261
Drawing in XNA is not thread-safe. You can not draw in anything but the main thread.Honeybunch
I am ONLY drawing in the main thread.user650261

1 Answers

1
votes

SpriteBatch.End is what actually calls draw. Everything else just pushes content into a queue to be rendered (IIRC). What I would guess is happening is that you are not using thread safe data structures between your threads. I understand you are only calling SpriteBatch.End on a single thread and no other threads have a SpriteBatch but if you are, say, updating the position of a Sprite on another thread while End is executing, you could be invalidating the render call. Have you checked your output window in Visual Studio? It may provide some detailed debugging information. Typically with threading you would have something like an AccessViolationException thrown when what I've described has happened. Maybe your system is just eating the exception? I don't know. I would say that we need to see what code is executing on these other threads to better identify what might be causing the issue.