| Next Tip?
Home » CSharp, Interview Questions, Tutorial

Threading Monitoring in C# – Easier to write

28 October 2008 9,244 views One Comment
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Threading Monitoring in C# – Easier to write

It’s a subsequent Code-Snippet of Article : Threading – A Step Ahead Story.

/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : gaurav.aroraose@yahoo.co.in */

// File name : threadingmonitor.cs

using System;
using System.Threading;

namespace CSharp.AStepAhead.threadingmonitor
{
class Alpha
{
public static string name,name1;
public void Beta()
{

//int iThreadID = Thread.CurrentThread.GetHashCode();
int cLoop = (name.Length >= name1.Length) ? name.Length - 1 : name1.Length - 1;
for (int i = 0; i <= cLoop; i++)
{
Monitor.Enter(this);
if (i <= name.Length - 1)
{
Console.Write(" {0} ", name.Substring(i, 1));
}
if (i <= name1.Length - 1)
{
Console.Write(" {0} ", name1.Substring(i, 1));
}
Console.WriteLine("\n");
Monitor.Exit(this);
}

}
}

class threading
{
public static void Main()
{
string name1, name2;
Console.Write("\n Enter First Thread Name: ");
name1=Console.ReadLine();

Console.Write("\n Enter Second Thread Name: ");
name2 = Console.ReadLine();
Console.Clear();

Console.WriteLine("\n Thread Syncronization Sample\n");
Console.WriteLine("\n First Thread Name : {0}",name1);
Console.WriteLine(" Second Thread Name : {0}", name2);

Console.WriteLine("\n Start Syncronization \n");
Console.ReadLine();

Alpha oAlpha = new Alpha();
Thread oThread1 = new Thread(new ThreadStart(oAlpha.Beta));
oThread1.Name = name1;
Alpha.name = name1;
Console.ForegroundColor = ConsoleColor.Green;
oThread1.Start();

while (!oThread1.IsAlive) ;

Thread oThread2 = new Thread(new ThreadStart(oAlpha.Beta));
oThread2.Name = name2;
Alpha.name1 = name2;
Console.ForegroundColor = ConsoleColor.Blue;
oThread2.Start();
while (oThread2.ThreadState == ThreadState.Unstarted) ;

Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Gray;
return;
}
}
}

Popularity: 9%

Post to Twitter Tweet This Post

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Ads by Lake Quincy Media

One Comment »

Leave your response!

You must be logged in to post a comment.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes