Click here to Skip to main content
15,880,608 members
Home / Discussions / C#
   

C#

 
Generalerror when docking one form to another Pin
Derek Smigelski30-Sep-02 19:05
Derek Smigelski30-Sep-02 19:05 
GeneralRe: error when docking one form to another Pin
David Stone30-Sep-02 19:17
sitebuilderDavid Stone30-Sep-02 19:17 
GeneralRe: error when docking one form to another Pin
Derek Smigelski30-Sep-02 19:18
Derek Smigelski30-Sep-02 19:18 
GeneralRe: error when docking one form to another Pin
Derek Smigelski30-Sep-02 19:28
Derek Smigelski30-Sep-02 19:28 
GeneralRe: error when docking one form to another Pin
David Stone1-Oct-02 11:29
sitebuilderDavid Stone1-Oct-02 11:29 
GeneralRe: error when docking one form to another Pin
James T. Johnson30-Sep-02 20:11
James T. Johnson30-Sep-02 20:11 
GeneralRe: error when docking one form to another Pin
James T. Johnson30-Sep-02 20:13
James T. Johnson30-Sep-02 20:13 
Generallock and entrancy Pin
Reno Tiko30-Sep-02 16:23
Reno Tiko30-Sep-02 16:23 
I ran the producer/consumer threading lock example, and set the breakpoints right after the cell locked. From my understanding, anything within a lock block should by synchronized, and only one thread can enter into a locked block at a time. However, when the program is run, the breakpoints are hit on both of the producer and consumer threads. How can this be?

The code used:

<br />
// MonitorSample.cs<br />
// This example shows use of the following methods of the C# lock keyword<br />
// and the Monitor class <br />
// in threads:<br />
//      Monitor.Pulse(Object)<br />
//      Monitor.Wait(Object)<br />
using System;<br />
using System.Threading;<br />
<br />
public class MonitorSample<br />
{<br />
	public static void Main(String[] args)<br />
	{<br />
		int result = 0;   // Result initialized to say there is no error<br />
		Cell cell = new Cell( );<br />
<br />
		CellProd prod = new CellProd(cell, 20);  // Use cell for storage, <br />
		// produce 20 items<br />
		CellCons cons = new CellCons(cell, 20);  // Use cell for storage, <br />
		// consume 20 items<br />
<br />
		Thread producer = new Thread(new ThreadStart(prod.ThreadRun));<br />
		Thread consumer = new Thread(new ThreadStart(cons.ThreadRun));<br />
		// Threads producer and consumer have been created, <br />
		// but not started at this point.<br />
<br />
		try<br />
		{<br />
			producer.Start( );<br />
			consumer.Start( );<br />
<br />
			producer.Join( );   // Join both threads with no timeout<br />
			// Run both until done.<br />
			consumer.Join( );  <br />
			// threads producer and consumer have finished at this point.<br />
		}<br />
		catch (ThreadStateException e)<br />
		{<br />
			Console.WriteLine(e);  // Display text of exception<br />
			result = 1;            // Result says there was an error<br />
		}<br />
		catch (ThreadInterruptedException e)<br />
		{<br />
			Console.WriteLine(e);  // This exception means that the thread<br />
			// was interrupted during a Wait<br />
			result = 1;            // Result says there was an error<br />
		}<br />
		// Even though Main returns void, this provides a return code to <br />
		// the parent process.<br />
		Environment.ExitCode = result;<br />
<br />
		Console.WriteLine("Finished");<br />
		Console.ReadLine();<br />
	}<br />
}<br />
<br />
public class CellProd<br />
{<br />
	Cell cell;         // Field to hold cell object to be used<br />
	int quantity = 1;  // Field for how many items to produce in cell<br />
<br />
	public CellProd(Cell box, int request)<br />
	{<br />
		cell = box;          // Pass in what cell object to be used<br />
		quantity = request;  // Pass in how many items to produce in cell<br />
	}<br />
	public void ThreadRun( )<br />
	{<br />
		for(int looper=1; looper<=quantity; looper++)<br />
			cell.WriteToCell(looper);  // "producing"<br />
	}<br />
}<br />
<br />
public class CellCons<br />
{<br />
	Cell cell;         // Field to hold cell object to be used<br />
	int quantity = 1;  // Field for how many items to consume from cell<br />
<br />
	public CellCons(Cell box, int request)<br />
	{<br />
		cell = box;          // Pass in what cell object to be used<br />
		quantity = request;  // Pass in how many items to consume from cell<br />
	}<br />
	public void ThreadRun( )<br />
	{<br />
		int valReturned;<br />
		for(int looper=1; looper<=quantity; looper++)<br />
			// Consume the result by placing it in valReturned.<br />
			valReturned=cell.ReadFromCell( );<br />
	}<br />
}<br />
<br />
public class Cell<br />
{<br />
	int cellContents;         // Cell contents<br />
	bool readerFlag = false;  // State flag<br />
	public int ReadFromCell( )<br />
	{<br />
		lock(this)   // Enter synchronization block<br />
		{<br />
			Console.WriteLine("Enter ReadFromCell()");<br />
			if (!readerFlag)<br />
			{            // Wait until Cell.WriteToCell is done producing<br />
				try<br />
				{<br />
					// Waits for the Monitor.Pulse in WriteToCell<br />
					Monitor.Wait(this);<br />
				}<br />
				catch (SynchronizationLockException e)<br />
				{<br />
					Console.WriteLine(e);<br />
				}<br />
				catch (ThreadInterruptedException e)<br />
				{<br />
					Console.WriteLine(e);<br />
				}<br />
			}<br />
			Console.WriteLine("Consume: {0}",cellContents);<br />
			readerFlag = false;    // Reset the state flag to say consuming<br />
			// is done.<br />
			Monitor.Pulse(this);   // Pulse tells Cell.WriteToCell that<br />
			// Cell.ReadFromCell is done.<br />
			Console.WriteLine("Exit ReadFromCell()");<br />
		}   // Exit synchronization block<br />
		return cellContents;<br />
	}<br />
   <br />
	public void WriteToCell(int n)<br />
	{<br />
		lock(this)  // Enter synchronization block<br />
		{<br />
			Console.WriteLine("Enter WriteToCell()");<br />
			if (readerFlag)<br />
			{      // Wait until Cell.ReadFromCell is done consuming.<br />
				try<br />
				{<br />
					Monitor.Wait(this);   // Wait for the Monitor.Pulse in<br />
					// ReadFromCell<br />
				}<br />
				catch (SynchronizationLockException e)<br />
				{<br />
					Console.WriteLine(e);<br />
				}<br />
				catch (ThreadInterruptedException e)<br />
				{<br />
					Console.WriteLine(e);<br />
				}<br />
			}<br />
			cellContents = n;<br />
			Console.WriteLine("Produce: {0}",cellContents);<br />
			readerFlag = true;    // Reset the state flag to say producing<br />
			// is done<br />
			Monitor.Pulse(this);  // Pulse tells Cell.ReadFromCell that <br />
			// Cell.WriteToCell is done.<br />
			Console.WriteLine("Exit WriteToCell()");<br />
		}   // Exit synchronization block<br />
	}<br />
}<br />

GeneralRe: lock and entrancy Pin
Eric Gunnerson (msft)1-Oct-02 11:50
Eric Gunnerson (msft)1-Oct-02 11:50 
GeneralRe: lock and entrancy Pin
Reno Tiko1-Oct-02 17:55
Reno Tiko1-Oct-02 17:55 
GeneralDistributing a C# App??? No Setup Tool Pin
Jon E30-Sep-02 16:03
Jon E30-Sep-02 16:03 
GeneralRe: Distributing a C# App??? No Setup Tool Pin
James T. Johnson30-Sep-02 16:24
James T. Johnson30-Sep-02 16:24 
Generalequivalent of VB's CreateObject in C#?... Pin
CherezZaboro30-Sep-02 8:17
CherezZaboro30-Sep-02 8:17 
GeneralRe: equivalent of VB's CreateObject in C#?... Pin
Paul Riley30-Sep-02 8:28
Paul Riley30-Sep-02 8:28 
GeneralRe: equivalent of VB's CreateObject in C#?... Pin
Richard Deeming1-Oct-02 0:45
mveRichard Deeming1-Oct-02 0:45 
GeneralTooltips by rectangle Pin
Nnamdi Onyeyiri30-Sep-02 6:41
Nnamdi Onyeyiri30-Sep-02 6:41 
GeneralRe: Tooltips by rectangle Pin
Stephane Rodriguez.30-Sep-02 7:05
Stephane Rodriguez.30-Sep-02 7:05 
GeneralRe: Tooltips by rectangle Pin
Nnamdi Onyeyiri30-Sep-02 7:09
Nnamdi Onyeyiri30-Sep-02 7:09 
GeneralSerial Number Field Pin
Nic Oughton30-Sep-02 5:12
professionalNic Oughton30-Sep-02 5:12 
GeneralRe: Serial Number Field Pin
leppie30-Sep-02 5:40
leppie30-Sep-02 5:40 
GeneralRe: Serial Number Field Pin
Nic Oughton30-Sep-02 6:04
professionalNic Oughton30-Sep-02 6:04 
GeneralRe: Serial Number Field Pin
leppie30-Sep-02 7:58
leppie30-Sep-02 7:58 
Generalblinking listbox Pin
Darryl Borden30-Sep-02 4:10
Darryl Borden30-Sep-02 4:10 
GeneralRe: blinking listbox Pin
Stephane Rodriguez.30-Sep-02 4:33
Stephane Rodriguez.30-Sep-02 4:33 
GeneralRe: blinking listbox Pin
Darryl Borden30-Sep-02 5:09
Darryl Borden30-Sep-02 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.