Create multithreaded applications

xiaoxiao2021-03-06  14

Since the previous period has been doing a silver-friendly project, which uses multi-threaded, and some basic technologies based on the socket underground communication, you are busy with you, put it here. The basic programming points and steps of Socket have been posted, and the basic knowledge of multi-threads is continued here.

Define Namespaces in .NET, multi-threaded features are defined in the System.ThReading namespace. Using system.threading;

Start the Thread class in the thread System.Threading Namespace represents a thread object, with this class object, you can create a new thread, delete, pause, and restore threads.

The following code creates a new thread using the Thread class, then launch this thread:

Thread mythread;

Mythread = New Thread (New ThreadStart (WriteData)); Mythread.Start ();

WriteData is a function to be executed by this thread, the code is as follows: protected str; for (int i = 0; i <= 10000; i ) {str = "secondary thread" i.toString () Console.writeline (listview1.listitems.count, str, 0, new string [] {"}); update ();}}

The Abort method of killing thread Thread is used to permanently kill a thread. However, please note that it is necessary to determine whether the thread is still activated before calling the Abort method,: if (mythread.isalive) {mythread.abort ();}

Tetable thread.sleep method is used to pause a thread for a while, the code is as follows: mythread.sleep (int);

To set up threads We can use the ThreadPriority property of the Thread Property attribute to set the thread priority. The range of thread priority is Normal, ABOVENORMAL, BELOWNORMAL, HIGHEST, or LOWEST. Please see the following settings:

Mythread.priority = threadpriority.Highest;

The SUSPEND method of the delay thread Thread class can delay a thread (suspended thread). The thread is delayed until the calling resume method.

IF (mythread.threadstate = threadstate.running) {mythread.suspend ();

Restoring the delayed thread calling the resume method can restore a delayed thread. If the thread is not delayed, the Resume method is invalid. IF (mythread.threadstate = threadstate.suspend) {mythread.resume ();}

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

New Post(0)