A preliminary study on the multi-threading mechanism of C# (2)

黄舟
Release: 2016-12-21 14:59:19
Original
1296 people have browsed it

Let’s create a thread next. When using the Thread class to create a thread, you only need to provide the thread entry. The thread entry allows the program to know what to let this thread do. In C#, the thread entry is provided through the ThreadStart delegate. You can understand ThreadStart as a function pointer, pointing to the function to be executed by the thread. When calling Thread. After the Start() method, the thread starts executing the function represented or pointed to by ThreadStart.

Open your VS.net and create a new console application (Console application). The following codes will let you experience the endless fun of fully controlling a thread!

//ThreadTest.cs

using System;
using System.Threading;

   public void Beta()

   {
    while (true)       {
    Console.WriteLine("Alpha.Beta is running in its own thread.");

  public class Simple
  {
   public static int Main()
{
    Console.WriteLine("Thread Start/Stop/Join Sample");

    Alpha oAlpha = new Alpha(); Thread(new ThreadStart(oAlpha.Beta));
    oThread.Start();

  while (!oThread.IsAlive);

   Thread.Sleep(1);    oThread.Join() ;
    Console.WriteLine();
    Console.WriteLine("Alpha.Beta has finished"); ;
    oThread.Start();
    }

     catch (ThreadStateException)

    {
      Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
  cannot be restarted.");
     Console.ReadLine();
     }
     return 0; , when the thread oThread we created calls the oThread.Start() method to start, the program actually runs the Alpha.Beta() method:

Alpha oAlpha = new Alpha();
 Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
 oThread.Start();

Then in the while loop of the Main() function, we use the static method Thread.Sleep() to stop the main thread for 1ms. During this time, the CPU Turn to the execution thread oThread. Then we try to use the Thread.Abort() method to terminate the thread oThread. Pay attention to the following oThread.Join(). The Thread.Join() method makes the main thread wait until the oThread thread ends. You can specify an int parameter as the maximum waiting time for the Thread.Join() method. Afterwards, we tried to use the Thread.Start() method to restart the thread oThread, but obviously the consequence of the Abort() method was unrecoverable termination of the thread, so in the end the program would throw a ThreadStateException

The above is the multi-threading mechanism of C# A preliminary exploration of the content of (2). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!