Better way to use a Condition (CancellationToken) Is there a better way to write this, as I am using the condition of the Cancellation Token to tell if it should use a byte[] from another thread or not.

        SharpDX.Windows.RenderLoop.Run(form, () =&gt;
        {
            if (CTS.IsCancellationRequested)
                form.Dispose();

            if (Queue.TryTake(out TextureData, 300))
            {
                Stoptimer = new Stopwatch();
                Stoptimer.Start();

                device.BeginScene();
                sprite.Begin(SpriteFlags.None);
                try
                {
                    using (var surface = texture.GetSurfaceLevel(0))
                    {
                        if (!CTS.IsCancellationRequested)
                            Surface.FromFileInMemory(surface, TextureData, SharpDX.Direct3D9.Filter.None, 0);
                    }
                    sprite.Draw(texture, new ColorBGRA(0xffffffff));
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Rendering: Texture");
                }

                sprite.End();
                device.EndScene();
                device.Present();
                Stoptimer.Stop();
                Console.WriteLine(Stoptimer.ElapsedTicks);
            }
        });


The important place is here:

        if (!CTS.IsCancellationRequested)
            Surface.FromFileInMemory(surface, TextureData, SharpDX.Direct3D9.Filter.None, 0);


As I need to recheck this, as the Cancellation can happen After it has taken the data from the Queue.

But I wonder, is there a better way to write it?

As you can see, I use the CTS condition 2 times, and it would be nice if I could only use it once to control the flow or something.
EDIT

I think this is what you are looking for, it will only check once inside the using block, if you want to dispose the form if that first Queue.TryTake(out TextureData, 300)) is false then add to the else of that statement.

if you really want to test it at the last second you could do it like this

 SharpDX.Windows.RenderLoop.Run(form, () =&gt;
    {
        if (Queue.TryTake(out TextureData, 300))
        {
            Stoptimer = new Stopwatch();
            Stoptimer.Start();

            device.BeginScene();
            sprite.Begin(SpriteFlags.None);
            try
            {
                using (var surface = texture.GetSurfaceLevel(0))
                {
                    if (!CTS.IsCancellationRequested)
                    {    
                        Surface.FromFileInMemory(surface, TextureData, SharpDX.Direct3D9.Filter.None, 0);
                    }
                    Else
                    {

                        // I imagine if you close the form that you want this stuff to 
                        //happen as well
                        sprite.End();
                        device.EndScene();
                        device.Present();
                        Stoptimer.Stop();
                        Console.WriteLine(Stoptimer.ElapsedTicks);
                        form.Dispose();
                    }
                }
                sprite.Draw(texture, new ColorBGRA(0xffffffff));
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Rendering: Texture");
            }

            sprite.End();
            device.EndScene();
            device.Present();
            Stoptimer.Stop();
            Console.WriteLine(Stoptimer.ElapsedTicks);
        }
        //Optional for Disposing of the form if Queue.TryTake(out TextureData, 300))
        // is false
        Else
        {
            form.Dispose();
        }
    });