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.
CancellationToken
here at all, since it looks like you're not actually using it for cancellation. \$\endgroup\$Surface.FromFileInMemory(surface, TextureData, SharpDX.Direct3D9.Filter.None, 0);
because everything else there will still happen no matter what, includingsprite.Draw(texture, new ColorBGRA(0xffffffff));
\$\endgroup\$cancellation token
to the one inside the doubleif
block? \$\endgroup\$