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