Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common operations of Thread threads

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you what the common operations of Thread threads are, which are concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Create thread

Threads are created by extending the Thread class. The extended Thread class calls the Start () method to start the execution of the child thread.

The following program demonstrates this concept:

Class ThreadCreationProgram {public static void CallToChildThread () {Console.WriteLine ("Child thread starts");} static void Main (string [] args) {ThreadStart childref = new ThreadStart (CallToChildThread); Console.WriteLine ("In Main: Creating the Child thread"); Thread childThread = new Thread (childref); childThread.Start () Console.ReadKey ();}}

When the above code is compiled and executed, it produces the following results:

In Main: Creating the Child thread Child thread starts

Management thread

The Thread class provides a variety of ways to manage threads.

The following example demonstrates the use of the sleep () method to pause a thread at a specific time.

Class ThreadCreationProgram {public static void CallToChildThread () {Console.WriteLine ("Child thread starts"); / / thread pauses for 5000 milliseconds int sleepfor = 5000; Console.WriteLine ("ChildThread Paused for {0} seconds", sleepfor / 1000); Thread.Sleep (sleepfor) Console.WriteLine ("Child thread resumes");} static void Main (string [] args) {ThreadStart childref = new ThreadStart (CallToChildThread); Console.WriteLine ("In Main: Creating the Child thread"); Thread childThread = new Thread (childref); childThread.Start (); Console.ReadKey ();}}

When the above code is compiled and executed, it produces the following results:

In Main: Creating the Child thread Child thread starts Child Thread Paused for 5 seconds Child thread resumes

Destroy thread

The Abort () method is used to destroy the thread.

Abort the thread at run time by throwing a threadabortexception. This exception cannot be caught, and if there is a finally block, the control is sent to the finally block.

The following procedure illustrates this:

Class ThreadCreationProgram {public static void CallToChildThread () {try {Console.WriteLine ("Child thread starts"); / / count to 10 for (int counter = 0; counter)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report