C # multithreading mechanism (2)

Source: Internet
Author: User

C # multithreading mechanism (2)

Next we will create a Thread. When using the Thread class to create a Thread, we only need to provide the Thread entry. The thread entry tells the program what to do with this thread. in C #, the thread entry is provided through the ThreadStart proxy (delegate). You can regard ThreadStart as a function pointer, point to the function to be executed by the Thread. after the Start () method, the thread starts to execute the Functions Represented or pointed to by ThreadStart.

Open your VS.net and create a Console Application. The following code will give you the pleasure of fully controlling a thread!

// ThreadTest. cs

Using System;
Using System. Threading;

Namespace ThreadTest
{
Public class Alpha
{
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 ();
        File: // ThisTo execute the Alpha Class Beta () method.
Thread oThread = new Thread (new ThreadStart (oAlpha. Beta ));
OThread. Start ();
While (! OThread. IsAlive );
Thread. Sleep (1 );
OThread. Abort ();
OThread. Join ();
Console. WriteLine ();
Console. WriteLine ("Alpha. Beta has finished ");
Try
{
Console. WriteLine ("Try to restart the Alpha. Beta thread ");
OThread. Start ();
}
Catch (ThreadStateException)
{
Console. Write ("ThreadStateException trying to restart Alpha. Beta .");
Console. WriteLine ("Expected since aborted threads cannot be restarted .");
Console. ReadLine ();
}
Return 0;
}
}
}

This program contains two classes: Alpha and Simple. when creating the thread oThread, we use the pointer to Alpha. the ThreadStart proxy (delegate) object is initialized in the Beta () method. When the created thread oThread calls oThread. when the Start () method is started, the actual program running is 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 1 ms. During this time, the CPU turns 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 (), Thread. Join () method to wait for the main Thread until the oThread Thread ends. You can specify an int-type parameter for the Thread. Join () method as the maximum waiting time. Later, we tried to use the Thread. Start () method to restart the Thread oThread, but obviously the consequence of the Abort () method is that the Thread cannot be recovered, so the program will throw a ThreadStateException.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.