如何安全地將事件傳遞到線程中。SlimDX / DX9 (How to safely pass events into a thread. SlimDX / DX9)


問題描述

如何安全地將事件傳遞到線程中。SlimDX / DX9 (How to safely pass events into a thread. SlimDX / DX9)

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 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 that 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.

So I decided to try rewriting the SDO class to use SlimDXs MessagePump.Run() functionality to render the scene constantly, however this causes exceptions because The SDO MessagePump.Run() conflicts with the MessagePump of the “Main Menu” WinForm (At least I think that is what this error message means: “Additional information: Starting a second message loop on a single thread is not a valid operation.”). I decided to try creating the SDO object in a new thread but then I can't communicate between the the TestObject and the SDO Object to add/remove/change the SO objects. I am not 100% sure that creating a multithreaded application is the best approach because I have read that DX and Threading leads to programing nightmares but I don't see any other solutions. I think I need to learn to use some kind of thread synchronization but I am very very new to threading and I want to make sure I am not heading down a road that is doomed to fail before I invest a long period of time learning to synchronized threads. If creating a multithreaded application is the best choice is there any DX/SlimDX threading issues I should be aware of? Any examples of simple SlimDX threaded programs that synchronize data across threads? Thanks for taking the time to read this post and I will extremely grateful to any advice you can give. 


參考解法

方法 1:

vanattab, why not checkout this online (free) book where you will find everything (well, most) you need to know about multi-threading on the .net platform. It's C# oriented, however the same techniques apply to VB.

http://www.albahari.com/threading/

(by Alexander Van Attauser1693593)

參考文件

  1. How to safely pass events into a thread. SlimDX / DX9 (CC BY-SA 3.0/4.0)

#slimdx #multithreading #vb.net #.net #directx






相關問題

使用紋理作為渲染目標時的大小限制 (Size limitation when using a texture as a render target)

如何安全地將事件傳遞到線程中。SlimDX / DX9 (How to safely pass events into a thread. SlimDX / DX9)

將directdx轉換為slimdx (Converting directdx to slimdx)

Toán véc tơ nhanh trong .NET - Các tùy chọn là gì? (Fast Vector Math in .NET - What are the options?)

DirectX10/ShapDX 自定義控件 (DirectX10/ShapDX Custom control)

使用 GDI 繪製散點圖太慢 (Drawing scatter plot is too slow with GDI)

防止 Direct3D 視口圖像縮放 (SlimDX) (Prevent Direct3D viewport image from scaling (SlimDX))

如何使用 SlimDX 設置 HLSL 統一變量的值? (How do I set the value of a HLSL uniform variable with SlimDX?)

使用 Direct3D 繪製帶有圓角末端的線條 (Drawing lines with rounded endings with Direct3D)

如何使圖元在 DirectX (2D) 中正確覆蓋精靈 (How to make primitives correctly overlay sprites in DirectX (2D))

多個渲染目標不保存數據 (Multiple Render Targets not saving data)

SlimDX VertexDeclaration 內存洩漏?- 如何避免? (SlimDX VertexDeclaration memory leak? - How to avoid it?)







留言討論