nightWatcher
nightWatcher

Reputation: 1051

Thread is still running or terminated exception in C#

This is my code. I have a queue that gets images. Images are retrieved in a loop, this means that queueForImages.count always increases. So when I try to run this program sometimes it throws this exception and sometimes not. I mean ive exited the application and now ive run it again. It will show this exception sometimes and sometimes not. I am new to threads. You can say I am a layman to know threads. So what am i missing? Do I have to dispose the thread or something before exiting application?

static void Main(string[] args)

        {

            Program obj = new Program();

            obj.handlerForImageBuffer();

        }

        void handlerForImageBuffer()

        {

            Bitmap mp = (Bitmap)Bitmap.FromFile(@"path.bmp");

            Thread imageThread = new Thread(new ThreadStart(processImages));

            for ( ; ; )

            {

                Console.WriteLine("Count: " + queueForImages.Count);

                if (queueForImages.Count == 10)

                {

                    queueFlag = true;                  

                    imageThread.Start();//Here comes the exception

                    Console.WriteLine("Q HAS 10 Elements");

                }
                queueForImages.Enqueue(Process(mp));//Same image is added for infinite times, just for the sake of testing.


            }

        }

Upvotes: 0

Views: 2124

Answers (3)

BrokenGlass
BrokenGlass

Reputation: 160892

BrokenGlass edited my post. I have used just one thread. which needs to be invoked when the images count reaches to 10. And dequeue these images. –

And there's the problem - when you de-queue an image from the queue it's item count is reduced by one - so if there were 11 images before, now there are 10 and then your

  if (queueForImages.Count == 10)

will cause the Exception you see. to solve it combine the other answers offered here by @Danny Chen and @dzendras):

  1. Use a ConcurrentQueue instead of whatever you are using now - regular queue is not thread safe.

  2. Create new Thread instance each time the Count == 10 because yes, this can occur multiple times since you add new images on the main thread, and remove them concurrently on a separate thread.

Upvotes: 1

dzendras
dzendras

Reputation: 4751

You cannot reuse the same thread object. It can run Start method only once. So if you want to runa method again, you have to create a new Thread object.

    void handlerForImageBuffer()

    {

        Bitmap mp = (Bitmap)Bitmap.FromFile(@"path.bmp");

        for ( ; ; )

        {

            Console.WriteLine("Count: " + queueForImages.Count);

            if (queueForImages.Count == 10)

            {

                queueFlag = true;                  

                Thread imageThread = new Thread(new ThreadStart(processImages));

                imageThread.Start();//Here comes the exception

                Console.WriteLine("Q HAS 10 Elements");

            }
            queueForImages.Enqueue(Process(mp));//Same image is added for infinite times, just for the sake of testing.


        }

    }

Upvotes: 2

Cheng Chen
Cheng Chen

Reputation: 43523

You should provide the code of your imageThread. Currently I guess you are using an instance of Queue<T> to store images, which is not thread-safe. If you are using C# 4.0 you can try ConcurrentQueue<T> that guarantees tread-safety. If you are using an older version, you can implement your own Queue<T> derived from the built-in one, and override the Enqueue/Dequeue method to make it thread-safe.

Upvotes: 2

Related Questions