# SlimDX Application Design Question (Threading issues and how to avoid them?)$$REWARD$$

## Recommended Posts

NOTE: For technical reasons I need to target DX9 (not 10/11), I am offering \$25 total via PayPal to any poster(s) who provide help leading to a resolution of this issue. I know that's not much but I am very poor and hopefully that is at least enough incentive to read my ridiculously long post.

I am having some trouble with figuring out how I should structure my program. I will first describe the general description of my program and then go into the things I have tried and the issues I have run into. I am not developing a game but rather a suite of computerized tests to test human vision. I am writing the program in VB.Net but converting between .Net languages is relativity trivial so if your going to post some code don't go out of your way to post it as VB if your better with C#. Even examples in other languages would be appreciated. I know the post is a little long but I want to be as clear as possible about the issues I am having. Thanks in advance!

Design: The program lunches a “Main Menu” VB.Net form that has lots of options to select different types of test and configure different options for the tests. When the user clicks the RunTest button for a specific test a TestObject representing that test is created. The TestObject constructor creates a StimulusDisplayObject(SDO) which is derived from the SlimDX RenderForm class. The job of the SDO class is to manage the SlimDX device object and render the different stimuli to the screen. It contains a list of StimulusObjects(SO) which are essentially just a DX surface that was loaded from an .bmp image and some location information that to tell the SDO class where to draw the surface on the screen. After the SDO is created the TestObject.BeginTest() method is called that is just a loop that presents a number of trials to the observer. The loop creates some SO objects and adds them to the list of SO objects of the SDO class to be displayed.

Problems: Because the stimuli I preset for the tests is fairly static and I usually only had to change the image that was displayed to the screen once every sec or two at the most, I did not have the scene rendering continually at as high of an FPS as possible. I simply had a SDO.RenderScene() that would get called from the TestObject whenever something need to be changed on the screen. This worked pretty well until I tried to handle DeviceLostExceptions and similar issues I arise from when the user ALT-TABs, CTRL-ALT-Delete, Covers the window (when in windowed mode), etc..) I have not found an effective way to handle these types of errors without having the scene continually rendering.

##### Share on other sites
Is the main menu continuing to run while the test takes place? You could potentially include the update and/or render call in the main menu's messagePump, wrapped in a boolean check to see if "test_is_running" is true?

##### Share on other sites
Yes it is. At least I think it is. When I am running the test one monitor and have the mainMenu form opened on the other monitor the mainMenu buttons and textboxes are still responsive. How would I go about adding the update call to the forms messagePump.

##### Share on other sites
Wait, is the main menu another SlimDX Form, or just a regular winform?

##### Share on other sites
Its a regular winform.

##### Share on other sites
There's nothing inherently wrong with only rendering when you need to. The reason games do it is because there are typical objects animating and camera movement constantly, so the changes have to be rendered anyway. If you give more information on what was going wrong with device lost scenarios, it might be a better approach to try to fix those and continue going the way you were before.

If you want to render continuously though, that's also viable. The idea is that you *replace* the Application.Run() method with MessagePump.Run(). You can run as many forms as you want on the same message loop. MessagePump.Run() internally hooks the application "Idle" event and constantly calls your provided callback whenever there are no window messages in the queue. You can put your rendering code inside that loop, and then you can add objects to be rendered to some shared list whenever you need to run one of your tests.

##### Share on other sites
[quote name='Mike.Popoloski' timestamp='1348674766' post='4984004']
There's nothing inherently wrong with only rendering when you need to. The reason games do it is because there are typical objects animating and camera movement constantly, so the changes have to be rendered anyway. If you give more information on what was going wrong with device lost scenarios, it might be a better approach to try to fix those and continue going the way you were before.

If you want to render continuously though, that's also viable. The idea is that you *replace* the Application.Run() method with MessagePump.Run(). You can run as many forms as you want on the same message loop. MessagePump.Run() internally hooks the application "Idle" event and constantly calls your provided callback whenever there are no window messages in the queue. You can put your rendering code inside that loop, and then you can add objects to be rendered to some shared list whenever you need to run one of your tests.
[/quote]

Thanks for the help Mike. I am working on rewritting some of the code now. I was haveing trouble trying to handle the DeviceLostExceptions when not rendering on demand. I tried to have some render code that looked something like:

[source lang="vb"]Sub RenderScene()
...Some Code
Try
device.present()
Catch e as devLostEx
DevLost = true
recover()
RenderScene()
End Try
End Sub

Sub recover()
While DevLost
Try
device.testCoop
Catch DevLostEx
'Do nothing
Catch DevNotReset
...Dispose defult pool stuff
device.reset()
recreate defult pool stuff
DevLost = false
End Try
End While
End Sub[/source]
That was with MDX9 not SlimDX. It just seemed to hang and never recover. I am not sure why. Anyway I have got a SlimDX basic class rendering contiually and handleing device lost exceptions fine. I am running it in its own thread and am just using synclock to protect data when I pass new objects to the display. I think the code would be a little slow if I need to render really fast because I synclock most of the SlimDX code but I don't think it will be a big issue for me. I am however running in to some trouble. I can't find a SlimDX replacment for the MDX9 Surface.FromImage() method. Also someone said that the DeviceEx class as some better features for passing data between threads so should I be using that class? Thanks for any help you can provide.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628326
• Total Posts
2982086

• 22
• 9
• 9
• 13
• 11