C # Key knowledge detailed (6: thread)

zhaozj2021-02-11  182

Like Java, writing a multi-thread application in C # is very simple, this chapter will introduce how to develop multi-thread programs in C # species. The thread in the .NET is defined by the System.Threading name space. So you must include this name space. Using system.threading;

Start a thread system.threading name space thread class describes a thread object, by using the class object, you can create, delete, stop, and restore a thread. Create a new thread through the new operation and you can start the thread through the start () method

Thread = New Thread (New ThreadStart (); THREAD.START (); Note: Different from the Java program, create a new thread and call the start () method, do not call the Run () method, but transfer the thread call. Below is a function that starts thread execution

Protected void helloworld () {string str; console.write ("helloWorld");}}

Kill a thread

The Abort () method of the thread class can be permanently killed a thread. Before killing a thread, you should judge whether the thread is in the survival.

IF (thread.isalive) {thread.abort ();

Stop a thread

Thread.sleep method can stop a thread in a fixed cycle class

Thread.sleep (); Setting Thread Priority

The threadPriority property in the thread class is used to set a priority of a ThreadPriority. Thread priority includes several Normal, ABOVENORMAL, BELOWNORMAL, HIGHEST, AND LOWEST.

Thread.priority = threadpriority.Highest;

Hang a thread

The Suspend () method of calling the thread class will hang a thread until you use the resume () method to evoke her. Before hanging a thread, it should be determined whether the thread is in the event.

IF (thread.threadstate = threadstate.running) {thread.suspend ();}

Avoch a thread

A suspended thread can be evoked by using the resume () method. Before hanging a thread, it should be determined whether the thread is hang during hanging, if the thread is not suspended, the method does not work.

IF (thread.threadstate = threadState.suspended) {thread.resume ();}

转载请注明原文地址:https://www.9cbs.com/read-4489.html

New Post(0)