give an example for suspending, resuming, and stopping a
thread ?

Answer Posted / tina

using System;
using System.Threading;

class MyThread {
public Thread thrd;

public MyThread(string name) {
thrd = new Thread(new ThreadStart(this.run));
thrd.Name = name;
thrd.Start();
}

// This is the entry point for thread.
void run() {
Console.WriteLine(thrd.Name + " starting.");

for(int i = 1; i <= 1000; i++) {
Console.Write(i + " ");
if((i%10)==0) {
Console.WriteLine();
Thread.Sleep(250);
}
}
Console.WriteLine(thrd.Name + " exiting.");
}
}

public class SuspendResumeStop {
public static void Main() {
MyThread mt1 = new MyThread("My Thread");

Thread.Sleep(1000); // let child thread start executing

mt1.thrd.Suspend();
Console.WriteLine("Suspending thread.");
Thread.Sleep(1000);

mt1.thrd.Resume();
Console.WriteLine("Resuming thread.");
Thread.Sleep(1000);

mt1.thrd.Suspend();
Console.WriteLine("Suspending thread.");
Thread.Sleep(1000);

mt1.thrd.Resume();
Console.WriteLine("Resuming thread.");
Thread.Sleep(1000);

Console.WriteLine("Stopping thread.");
mt1.thrd.Abort();

mt1.thrd.Join(); // wait for thread to terminate

Console.WriteLine("Main thread terminating.");
}
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can a abstract class have a constructor?

466


What is IL / CIL / MSIL?

667


What is a interface in c#?

475


Explain the difference between the debug class and trace class?

498


What is the advantage of static class in c#?

511






what is the default access for a class

580


What is dynamic dispatch?

552


What are get and set in c#?

581


How to implement an object pool in c#.net.

552


How can you write a class to restrict that only one object of this class can be created (Singleton class)?

519


How big is an int16?

494


What is cts, clr?

435


For read-only operation which property you have to designated?

557


What is readline library?

504


How do you declare an interface in c#?

488