• System.Threading.Thread.Interrupt()

    I was reading about thread.Interrupt on msdn and wasn't quite understanding where to trap the ThreadInterruptedException that was supposedly thrown on the worker thread. I believe the trick is that the Thread.Join() or the Thread.Sleep() functions are what throw the exception. Take for example this code:
     
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
     
    namespace ThreadInterruptingLesson
    {
     
        class Program
        {
            static void Main(string[] args)
            {
                Thread worker = new Thread(ThreadProc);
                worker.Start();
               
                worker.Interrupt();
                Console.WriteLine("Finished");
                Console.ReadKey();
            }
            static void ThreadProc()
            {
                try
                {
                    Thread.Sleep(Timeout.Infinite);
                }
                catch (ThreadInterruptedException)
                {
                    Console.WriteLine("Out of sleep");
                }
            }
        }
    }
     
     
    Now using Interrupt() to break a thread.Join() call was a little  more convoluted to me, but this is what i came up with for an example:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
     
    namespace ThreadInterruptingLesson
    {
     
        class Program
        {
            static void Main(string[] args)
            {
                Thread worker = new Thread(ThreadProc);
                worker.Start();
               
                worker.Interrupt();
     
     
                Console.ReadKey();
                worker.Interrupt();
     
                Console.ReadKey();
            }
            static void ThreadProc()
            {
                try
                {
                    Thread.Sleep(Timeout.Infinite);
                }
                catch (ThreadInterruptedException)
                {
                    Console.WriteLine("proc1 sleep was interrupted, press any key to interrupt proc1.Join");
                }
                Thread worker2 = new Thread(ThreadProc2);
                worker2.Start();
                try
                {
                    worker2.Join();
                }
                catch (ThreadInterruptedException)
                {
                    Console.WriteLine("proc1.Join() was interrupted, so we're calling interrupt on proc2.sleep now");
                    worker2.Interrupt();
                }
            }
            static void ThreadProc2()
            {
                try
                {
                    Thread.Sleep(Timeout.Infinite);
                }
                catch (ThreadInterruptedException)
                {
                    Console.WriteLine("Proc2 sleep was interrupted, press any key to exit");
                }
            }
        }
    }
     
    I think this example needs a little explanation
     
    first i start up ThreadProc() in a worker thread. It is sitting there with a sleep() going infinitely.
     
    When i call interrupt() on it from the main thread, a ThreadInterruptedException is thrown Inside the worker thread (ThreadProc()).
    At this point ThreadProc continues on it's execution which leads it to creating a second worker thread (so now theres 3 threads, the main thread, a worker that we have a reference to in our main thread, and a third thread where that we're referencing in the first worker thread).
     
    Inside ThreadProc we call Join() on the second worker thread (ThreadProc2() while it's sitting there on an infinite sleep()).
     
    This means ThreadProc is blocking (until ThreadProc2 completes, which it won't ever complete unless we call Interrupt() on it from ThreadProc).
     
    If you're still with me thus far, you're doing 10x better than i was.
     
    Anyhow now in the main thread we call Interrupt() on the first worker thread (ThreadProc) again, which throws ThreadInterruptedException on the worker2.Join() call.
    Since we don't want our ThreadProc2 sitting out there forever, i went ahead and did an Interrupt on it also so that it completed execution.
     
    Hope this makes Thread.Interrupt() a little more clear for everyone.

    Full story

    Comments (0)

  • using DeflateStream or GZipStream

    basically this is the examples of msdn thrown together, seperated by a preprocessor define (kickin it c++ old school

     
    #define DEFLATE
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.IO.Compression;
     
    namespace DeflateLesson2
    {
     
        class Program
        {
     
            static void Main(string[] args)
            {
     
                byte[] data = new byte[10240000];
                FillDataWithZeroes(data);
                using (MemoryStream ms = new MemoryStream())
                {
    #if DEFLATE
                    using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress,true))
    #else
                    using (GZipStream ds = new GZipStream(ms, CompressionMode.Compress, true))
    #endif
                    {
                        Stuff(data, ds);
                        Console.WriteLine("Uncompressed: {0} bytes", data.Length);
                        Console.WriteLine("Compresseed: {0} bytes ", ms.Length);
     
                    }
     
     
                    ms.Position = 0;
    #if DEFLATE
                    using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress))
    #else
                    using (GZipStream ds = new GZipStream(ms, CompressionMode.Decompress))
    #endif
                    {
                        byte[] buffer = new byte[data.Length + 100];
                        int offset = 0;
                        int actuallyRead =0;
     
                        while (true)
                        {
     
                            int bytesRead = ds.Read(buffer, offset, 100);
                            if (bytesRead == 0) break;
                            actuallyRead += bytesRead;
                            offset += 100;
     
                        }
                        Console.WriteLine("Bytes actually read: {0} bytes", actuallyRead);
                    }
                }
                Console.ReadKey();
            }
    #if DEFLATE
            private static void Stuff(byte[] data, DeflateStream ds)
    #else
            private static void Stuff(byte[] data, GZipStream ds)
    #endif
            {
                ds.Write(data, 0, data.Length);
            }
     
            private static void FillDataWithZeroes(byte[] data)
            {
                for (int x = 0; x < data.Length; x++)
                {
                    data[x] = new byte();
                    data[x] = (byte)(x % 5);
                }
            }
        }
    }

     

     

    Full story

    Comments (0)