Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Wednesday, June 9, 2010

Managing Threads Simple Example using C#.NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ManageThreadExample
{
class Program
{
public static int i = 0;
static void Main(string[] args)
{


Thread DisplayThread = new Thread(new ThreadStart(display));
DisplayThread.Start();
Console.WriteLine("Main Thread is sleeping for 20 seconds");
Thread.Sleep(20000);
Console.WriteLine("DisplayThread is suspended for 5 seconds");
DisplayThread.Suspend();
Thread.Sleep(5000);
Console.WriteLine("DisplayThread is resumed for 40 seconds");
DisplayThread.Resume();
Thread.Sleep(20000);
Console.WriteLine("DisplayThread is Aborted");
DisplayThread.Abort();
Thread.Sleep(3000);




}

public static void display()
{

for (i = 1; i < 10; i++)
{


Console.WriteLine("Displaying while the thread stops me for " + i + " time/times");

int sleeptime = i * 1000;
Thread.Sleep(sleeptime);
int Totaltime = 0;
Totaltime = Totaltime + i;
Console.WriteLine("Sleeping for " + Totaltime + " Seconds");
int TimeThroughout = Totaltime + i + (sleeptime / 1000);
Console.WriteLine("Time spent so far : " + TimeThroughout );

}



}


}
}

No comments:

Post a Comment