Threading Monitoring in C# - Easier to write

Threading Monitoring in C# - Easier to write
It’s a subsequent Code-Snippet of Article : Threading CSharp - A Step Ahead Series


/* 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;
}
}
}


Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 157 articles authored by Gaurav Aroraa

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: