Thread class the basics of C # a variety of ways to create a thread

1. Thread class to create a thread

The constructor parameter ThreadStart delegate acceptable (no parameters defined function returning void), and ParameterizedThreadStart delegate parameter (defined Object parameters, the function returns a void).

 

        static void Main(string[] args)
        {
            
            Console.WriteLine("Main Start....");
            Thread t1 = new Thread(Calculate);
            t1.Start(10);
            Console.WriteLine("Main is over");
            Console.ReadLine();
        }

        private static void Calculate(object obj)
        {
            int sum = 0;
            int total = (int)obj;
            for (int i = 0; i < total;i++ )
            {
                sum += i;
                Thread.Sleep(100);
            }
             Console.WriteLine("Sum is "+sum);
        }

 

Thread class can of course also be used with Lamda expressions, to achieve the same function as above:

        static void Main(string[] args)
        {
            
            Console.WriteLine("Main Start....");
            Thread t1 = new Thread((obj) =>
            {
                int sum = 0;
                int total = (int)obj;
                for (int i = 0; i < total; i++)
                {
                    sum += i;
                    Thread.Sleep(100);
                }
                Console.WriteLine("Sum is " + sum);
            });
            t1.Start(10);
            Console.WriteLine("Main is over");
            Console.ReadLine();
        }

2. background thread

As long as there is a run in the foreground, the application process will not stop running, and running background thread does not affect the end of the application process, before, there was no difference in background thread thread priorities and other aspects. By default, Thread class to create a thread is the foreground thread, you can also change the background thread by setting IsBackground property, and create the thread pool are background threads. Below, the Console.ReadLine (); command is disabled, run discovery console window will not disappear immediately, but will wait for the end t1 thread running.

 

        static void Main(string[] args)
        {
            
            Console.WriteLine("Main Start....");
            Thread t1 = new Thread((obj) =>
            {
                int sum = 0;
                int total = (int)obj;
                for (int i = 0; i < total; i++)
                {
                    sum += i;
                    Thread.Sleep(100);
                }
                Console.WriteLine("Sum is " + sum);
            });
            t1.Start(10);
            Console.WriteLine("Main is over");
            //Console.ReadLine();
        }

 

3. Thread Priority

Thread priority by Priority property represents, defined levels are Highest, AboveNormal, BelowNormal, Lowest, the operating system thread scheduling according to priority. If you have the same priority several threads running, the lingua franca of the rules on the amount of time and cycles. If the thread is CPU-intensive (CPU does not need to have to wait), priority to below a defined base priority.

4. The thread control and status

 ThreadState thread defines a set of all possible execution state . Once the thread is created, it is at least in one state until terminated. Because it is a combination of various states, it is determined that the current state of the thread when you need to use bit operation, and can not simply use the equal sign (There is an exception, Runing state value is 0, it is not by the operation).

 Unstarted: create a thread but to call Run ();

 Runing: After calling Run (), when the thread is being called CPU;

WaitSleepJoin: thread waiting, call Sleep (), Join () will enter the state in which the Join () blocks the current thread until the end or suspend the calling thread is running;

Stopped: After the thread running into the state, this state can not pass Start (), re-start threads, you must re-create the thread;

Background: After the current calling thread after IsBackground property of the thread after is true, a thread is created, the status is Unstarted + Background, and after calling thread state Runing + Background, sleep () status WaitSleepJoin + Background, after the end of the thread state Stopped;

AbortRequested: When the thread calls Abort (), finish processing before ThreadAbortException, then you can call ResetAbort (), cancel the suspension of the operation;

Aborted: When the thread calls Abort (), processing After ThreadAbortException;

SuspendRequested: When the thread calls Suspend (), the thread is suspended before;

Suspended: the threads have been suspended at this time available Resume (), continue to run;

The common thread of control function is basically already mentioned above, which Suspend (), Resume () is no longer recommended, because the thread hangs may cause some unpredictable problems, such as thread deadlocks, you can use some of the thread synchronization class instead. In addition, Abort () is not recommended, you can define a Boolean state amount instead. Interrupt () not mentioned above, after calling this function, there will be InterruptException, then wake up the thread in WaitSleepJoin state.

Guess you like

Origin www.cnblogs.com/change-myself/p/11111259.html