C # multi-line instances

Source: Internet
Author: User

Using System;
Using System. Threading;
Using System. Text;

Namespace ControlThread
{
Class Program
{
Private static void DisplayMessage ()
{

// Display a message repeatedly on the console.
While (true)
{

Try
{

Console. WriteLine ("{0}: The second thread is running. Enter"
+ "(S) uspend, (R) esume, (I) nterrupt, or (E) xit .",
DateTime. Now. ToString ("HH: mm: ss. ffff "));

// Two seconds of sleep.
Thread. Sleep (2000 );

}
Catch (ThreadInterruptedException)
{

// The thread is interrupted. After the ThreadInterruptedException exception is caught
// The example executes the appropriate action and continues execution.
Console. WriteLine ("{0}: The second thread is interrupted .",
DateTime. Now. ToString ("HH: mm: ss. ffff "));

}
Catch (ThreadAbortException abortEx)
{

// ThreadAbortException object.
// The ExceptionState attribute provided by the Thread after the Thread. Abort is called
// In this example, the string contains a reason for termination.
Console. WriteLine ("{0}: The second thread is terminated. ({1 })",
DateTime. Now. ToString ("HH: mm: ss. ffff "),
AbortEx. ExceptionState );

// Although ThreadAbortException has been processed, www.2cto.com
// An exception is thrown during running to ensure the thread is terminated.

}
}

}

Static void Main (string [] args)
{
// Create a new thread object,
// Pass it to the ThreadStart delegate real column that executes the Display message method.

Thread thread = new Thread (new ThreadStart (DisplayMessage ));

Console. WriteLine ("{0}: start the second thread .",
DateTime. Now. ToString ("HH: mm: ss. ffff "));

// Start the second thread.
Thread. Start ();

// Process user input commands cyclically.
Char command = '';

Do
{

String input = Console. ReadLine ();
If (input. Length> 0) command = input. ToUpper () [0];
Else command = '';

Switch (command)
{

Case's ':
// Suspend the second thread.
Console. WriteLine ("{0}: suspends the second thread .",
DateTime. Now. ToString ("HH: mm: ss. ffff "));
Thread. Suspend ();
Break;

Case 'r ':
// Continue the second thread.
Try
{
Console. WriteLine ("{0}: continue the second thread .",
DateTime. Now. ToString ("HH: mm: ss. ffff "));
Thread. Resume ();
}
Catch (ThreadStateException)
{
Console. WriteLine ("{0}: The thread is not suspended .",
DateTime. Now. ToString ("HH: mm: ss. ffff "));
}
Break;

Case 'I ':
// Interrupt the second thread.
Console. WriteLine ("{0}: The second thread is interrupted .",
DateTime. Now. ToString ("HH: mm: ss. ffff "));
Thread. Interrupt ();
Break;

Case 'E ':
// Terminate the second thread and pass a State object to the aborted thread
// This example is a message.
Console. WriteLine ("{0}: terminating the second thread .",
DateTime. Now. ToString ("HH: mm: ss. ffff "));

Thread. Abort ("termination example .");

// Wait until the second thread ends.
Thread. Join ();
Break;
}
} While (command! = 'E ');

// Wait for the task to continue.
Console. WriteLine ("the main method ends. Press enter .");
Console. ReadLine ();

}
}
}

 

From rayhuang110's column

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.