// creating and starting threads
using System;
using System.Threading;
public class AOne
{
public void First()
{
Console.WriteLine("First methode of AOne class is running on T1 thread.");
Thread.Sleep(1000);
Console.WriteLine("The First method called by T1 thread has ended.");
}
public static void Second()
{
Console.WriteLine("First methode of AOne class is running on T1 thread.");
Thread.Sleep(2000);
Console.WriteLine("The First method called by T1 thread has ended.");
}
}
public class ThreadExp
{
public static int Main(string[] args)
{
Console.WriteLine("Example of Threading");
AOne a = new AOne();
Thread T1 = new Thread(new ThreadStart(a.First)); //creating thread T1
T1.Start(); //Strating thread T1
Console.WriteLine("T1 thread started.");
Thread T2 = new Thread(new ThreadStart(AOne.Second)); //creating thread T2
T2.Start(); //starting thread T2
Console.WriteLine("T2 thread started.");
return 0;
}
}
_____________________________________________________________________________________
// 2nd Program------using timer
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace ThreadTimers
{
class Program
{
static void Main()
{
Timer time = new Timer(); //Doesn't require any args
time.Interval = 1000;
time.Elapsed += time_Elapsed; //uses an event
time.Start(); //start the timer
Console.ReadLine();
time.Stop();
Console.WriteLine("Timer Thread stopped. Enter again to start. \n Twice entering would
destroy the thread."); //pause the timer
Console.ReadLine();
time.Start(); //Resume the timer
Console.ReadLine();
Console.WriteLine("Thread destroyed!");
time.Dispose(); //Destroy the timer Thread
}
static void Display(object ObjTime)
{
//This runs on a pooled thread
Console.WriteLine(ObjTime); //Writes "tick..."
}
static void time_Elapsed (object sender, EventArgs e)
{
Console.Write("The Time is passing by: ");
Console.WriteLine(DateTime.Now);
}
}
}
using System;
using System.Threading;
public class AOne
{
public void First()
{
Console.WriteLine("First methode of AOne class is running on T1 thread.");
Thread.Sleep(1000);
Console.WriteLine("The First method called by T1 thread has ended.");
}
public static void Second()
{
Console.WriteLine("First methode of AOne class is running on T1 thread.");
Thread.Sleep(2000);
Console.WriteLine("The First method called by T1 thread has ended.");
}
}
public class ThreadExp
{
public static int Main(string[] args)
{
Console.WriteLine("Example of Threading");
AOne a = new AOne();
Thread T1 = new Thread(new ThreadStart(a.First)); //creating thread T1
T1.Start(); //Strating thread T1
Console.WriteLine("T1 thread started.");
Thread T2 = new Thread(new ThreadStart(AOne.Second)); //creating thread T2
T2.Start(); //starting thread T2
Console.WriteLine("T2 thread started.");
return 0;
}
}
_____________________________________________________________________________________
// 2nd Program------using timer
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace ThreadTimers
{
class Program
{
static void Main()
{
Timer time = new Timer(); //Doesn't require any args
time.Interval = 1000;
time.Elapsed += time_Elapsed; //uses an event
time.Start(); //start the timer
Console.ReadLine();
time.Stop();
Console.WriteLine("Timer Thread stopped. Enter again to start. \n Twice entering would
destroy the thread."); //pause the timer
Console.ReadLine();
time.Start(); //Resume the timer
Console.ReadLine();
Console.WriteLine("Thread destroyed!");
time.Dispose(); //Destroy the timer Thread
}
static void Display(object ObjTime)
{
//This runs on a pooled thread
Console.WriteLine(ObjTime); //Writes "tick..."
}
static void time_Elapsed (object sender, EventArgs e)
{
Console.Write("The Time is passing by: ");
Console.WriteLine(DateTime.Now);
}
}
}
No comments:
Post a Comment