Monitor.Pulse(this) does not trigger Monitor.Wait(this);
<p>I'm trying to get Monitor.Pulse(this) to trigger Monitor.Wait(this) in my code.  I think my Wait statements are all running at some point with no Pulse.  I have 5 different threads run by 5 different objects, each representing a queue with different priority.  I'm trying to get each thread to run with a certain priority without using the thread priority attribute (i.e. normal, abovenormal, etc.).  Anyways, point is that each thread only runs once and then it seems they are stuck at the Monitor.Wait(this) part in the thread that runs for each queue.  Does anyone know why the Monitor.Pulse(this) doesn't trigger the Monitor.Wait(this) and continue the cycle.  Each thread should be triggered one after the other by the Monitor.Wait(this) and the while loop that uses the Global variable GlobalCount.  I think the problem must occur in my Beta method in the first class (Msg class) at the top where this triggering occurs.  Or in my main method, although I'm less sure of that part having an issue.</p>

<p>What happens is it will execute a few lines and then start a new line but won't print anything else. The code is still running.  I also tried removing the Monitor.Pulse and Monitor.Wait and it partially works, but every time the delta object's beta method runs its thread it is replaced by the alpha method.  Does anyone know why this is and how I can get Pulse and Wait to work?</p>

<p>Here is my code (ignore some of the comments):</p>

<pre><code>   // StopJoin.cs
using System;
using System.Threading;
using System.Collections;



public class Msg
{
string message;
int priority;

public Msg(string ms, int pr)
{message = ms;
priority = pr;}


// This method that will be called when the thread is started
public void Beta()
{



while(true){

//Console.WriteLine("asdfasdfs");
Console.WriteLine(message+":"+GlobalClass.globalCount);
lock(this)   // Enter synchronization block
{
while((priority - 1) != GlobalClass.globalCount){
//Console.WriteLine(GlobalClass.globalCount);
try
{
// Waits for the Monitor.Pulse in WriteToCell
//Console.WriteLine("beginning");
//Monitor.Wait(this);
//Console.WriteLine("end");
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
if(GlobalClass.globalCount &gt;= 5)
    GlobalClass.globalCount = 0;
}
Console.WriteLine(message+".Beta is running in its own thread.");

for(int i = 0;i&lt;priority;i++)
{
Console.WriteLine("sending message...");

}

if(GlobalClass.globalCount &lt; 5)
    GlobalClass.globalCount = GlobalClass.globalCount + 1;


//Monitor.Pulse(this);   // Pulse tells Cell.WriteToCell that
//Console.WriteLine(GlobalClass.globalCount);
}
}
}
}






public class Alpha
{
Msg the_message = new Msg("Alpha",1);

public void doWork()
{the_message.Beta();}
};




public class Charlie
{
Msg the_message = new Msg("Charlie",2);
public void doWork()
{the_message.Beta();}
};






public class Delta
{
Msg the_message= new Msg("Alpha",3);
public void doWork()
{the_message.Beta();}
};





public class Echo
{
Msg the_message= new Msg("Echo",4);
public void doWork()
{the_message.Beta();}
};







public class Foxtrot
{
Msg the_message= new Msg("Foxtrot",5);
public void doWork()
{the_message.Beta();}
};




static class GlobalClass
{
private static int global_count = 0;

public static int globalCount
{
get{return global_count;}
set{global_count = value;}
}
}






public class Simple
{
public static int Main()
{

GlobalClass.globalCount = 2;

long s = 0;
long number = 100000000000000000;

Console.WriteLine("Thread Start/Stop/Join Sample");

Alpha oAlpha = new Alpha();
Charlie oCh = new Charlie();
Delta oDe = new Delta();
Echo oEc = new Echo();
Foxtrot oFo = new Foxtrot();

// Create the thread object, passing in the Alpha.Beta method
// via a ThreadStart delegate. This does not start the thread.
Thread oThread = new Thread(new ThreadStart(oAlpha.doWork));
Thread aThread = new Thread(new ThreadStart(oCh.doWork));
Thread bThread = new Thread(new ThreadStart(oDe.doWork));
Thread cThread = new Thread(new ThreadStart(oEc.doWork));
Thread dThread = new Thread(new ThreadStart(oFo.doWork));

// Start the thread
oThread.Start();
aThread.Start();
bThread.Start();
cThread.Start();
dThread.Start();

// Spin for a while waiting for the started thread to become
// alive:
while (!oThread.IsAlive);
while (!aThread.IsAlive);
while (!bThread.IsAlive);
while (!cThread.IsAlive);
while (!dThread.IsAlive);

// Put the Main thread to sleep for 1 millisecond to allow oThread
// to do some work:
Thread.Sleep(1);



// Wait until oThread finishes. Join also has overloads
// that take a millisecond interval or a TimeSpan object.
oThread.Join();
aThread.Join();
bThread.Join();
cThread.Join();
dThread.Join();

Console.WriteLine();
Console.WriteLine("Alpha.Beta has finished");

/*
try 
{
Console.WriteLine("Try to restart the Alpha.Beta thread");
oThread.Start();
}
catch (ThreadStateException) 
{
Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
Console.WriteLine("Expected since aborted threads cannot be restarted.");
}
*/


while(s&lt;number)
s++;

// Request that oThread be stopped
oThread.Abort();
aThread.Abort();
bThread.Abort();
cThread.Abort();
dThread.Abort();


return 0;
}
}
</code></pre>
