Sponsored Ad

Sunday, March 13, 2011

How to Start and Stop a Thread using C#

This is simple C# thread program which start and stop the thread. As you can see that after stropping the thread it is not printing next statement.

How to Start and Stop a Thread using C#

C# Program Example for Thread start and stop:

using System;
using System.Threading;
namespace MyApp
{
    class thread_class
    {
        static void Main()
        {
            ThreadStart ts = new ThreadStart(myThreadFunction);
            Console.WriteLine("Creating thread");

            Thread t = new Thread(ts);
            t.Start();

            Console.WriteLine("Starting thread");           

            Console.Read();
        }

        static void myThreadFunction()
        {
            Console.WriteLine("Calling thread");
            Thread.CurrentThread.Abort();
            Console.WriteLine("After stop thread");
        }
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates