2
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ I think you shouldn't use CancellationToken here at all, since it looks like you're not actually using it for cancellation. \$\endgroup\$
    – svick
    Aug 26, 2013 at 17:33
  • \$\begingroup\$ 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). \$\endgroup\$
    – Zerowalker
    Aug 26, 2013 at 18:26
  • \$\begingroup\$ 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)); \$\endgroup\$
    – Malachi
    Aug 26, 2013 at 19:03
  • \$\begingroup\$ how much time is really passing between the first mention of the cancellation token to the one inside the double if block? \$\endgroup\$
    – Malachi
    Aug 26, 2013 at 19:04

1 Answer 1

1
\$\begingroup\$

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();
        }
    });
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Ah good means to use the Else statement, helped me out, thanks:) \$\endgroup\$
    – Zerowalker
    Aug 26, 2013 at 23:40
  • \$\begingroup\$ your welcome, looks like you are writing a lot of code from all the posts, glad that I can help \$\endgroup\$
    – Malachi
    Aug 27, 2013 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.