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, () =>
{
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.

• I think you shouldn't use CancellationToken here at all, since it looks like you're not actually using it for cancellation. Aug 26, 2013 at 17:33
• I am, but well, it´s like this. I have 2 Threads, one fills a buffer, one reads it (this is the reading part). So they must be in sync more or less. So i need to be able to shut them down at the same time. And also be able to close it along with the other if the connection fails (the other thread is using TCP). Aug 26, 2013 at 18:26
• ok so the only thing that you don't want it to do if the cancellation token is activated is Surface.FromFileInMemory(surface, TextureData, SharpDX.Direct3D9.Filter.None, 0); because everything else there will still happen no matter what, including sprite.Draw(texture, new ColorBGRA(0xffffffff));
– Malachi
Aug 26, 2013 at 19:03
• how much time is really passing between the first mention of the cancellation token to the one inside the double if block?
– Malachi
Aug 26, 2013 at 19:04

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, () =>
{
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();
}
});

• Ah good means to use the Else statement, helped me out, thanks:) Aug 26, 2013 at 23:40
• your welcome, looks like you are writing a lot of code from all the posts, glad that I can help
– Malachi
Aug 27, 2013 at 13:20