Skip to main content
gamedev.net
🔒 Locked

DeviceLostException

Started by AdamWebb Oct 15, 2005 at 2:39 PM 3 replies 2.5k views
Original Post
AdamWebb
AdamWebb
I was just working on fine tuning my Managed DirectX framework when I noticed something that I hadn't up until now. I was using this rendering function (bare-bones & straight out of Microsoft's DirectX SDK Docs):

private void RenderDirect3D()
{
    if (device == null) 
    {
        return;
    }

    device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
    device.BeginScene();

    device.EndScene();
    device.Present();
}

The trouble is; if I switch application focus on my desktop, say to Internet Explorer or Media Player, those applications start to stick and my computer will slowly start to grind to a halt. Visual C# spits out an error concerning a DeviceLostException. I've worked out a solution to this (The name of the rendering device changed from 'device' to 'd3dDevice'):

private void RenderDirect3D()
{
    try
    {
        d3dDevice.Clear( ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0 );
        d3dDevice.BeginScene();

        d3dDevice.EndScene();
        d3dDevice.Present();
    }
    catch( DeviceLostException )
    {
        return;
    }

    return;
}

This works but I get the feeling there is a better way of doing it. None of the examples of Managed DirectX code I can find handle this problem, or even mention what to do when you want to write a MDX application that may loose it's focus. Has anyone else had to deal with this and am I dealing with it 'correctly'?
LosBrutalos
LosBrutalos
A possibility is, if your app catch this error, its a good idea to give
the other running appications a litte more time....

Calling "System.Threading.Thread.Sleep(10)"

may be the solution.
Please let me know if i bet right...

-- keep brutal --
+Los Brutalos+
Los Brutalos(Sorry for my brutal english ;)
Muhammad Haggag
Muhammad Haggag
Other applications slow down because you're spinning in a loop that does nothing but throwing and catching an exception. To quote the managed documentation:
Quote:
The remarks section of Device.TestCooperativeLevel
If the device is lost and cannot be restored at the current time, Device.TestCooperativeLevel throws a DeviceLostException. This is the case, for example, when a full-screen device loses focus. If an application detects a lost device, it should pause and periodically call Device.TestCooperativeLevel until no exceptions are thrown. The application can then attempt to reset the device by calling Device.Reset and, if this succeeds, restore the necessary resources and resume normal operation. Note that Device.Present throws a DeviceLostException if the device is either "lost" or "not reset".


So, you can do something like this (off the top of my head):
private void RenderDirect3D(){    try    {        d3dDevice.TestCooperativeLevel();        d3dDevice.Clear( ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0 );        d3dDevice.BeginScene();        d3dDevice.EndScene();        d3dDevice.Present();    }    catch( DeviceLostException )    {        System.Threading.Thread.Sleep(500); // Sleep for a while    }    catch(DeviceNotResetException)    {        // Restore the device    }}

Armadon
Armadon
Just to add what Coder said,
You will need to have the app sleep for a bit so that the device will give you the DeviceNotReset exception.
I wrote a simple but hopefully informative article on handling lost devices in managed directx.

I hope this helps. It just goes through some of the basics and might lack some clear explanations.
Take care.
AdamWebb
AdamWebb
Fantastic everything works good now.

Thanks for posting Armadon, been looking for those links in your sig. for ages :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.