C# ThreadPool MannualResetEvent Example

Posted on 2012-09-05
Last Modified: 2012-09-05

I have a few simple questions? Firstly in C# when you pass a variable to function is it passed as a reference or value by default. I understand that classes are reference types so if you pass a class to a function is this always passed as a reference?

The reason I ask is because I'm confussing myself when using the ThreadPool and the MannualResetEvent. Please see the code below. I pass a mannualresetevent to another class and in that class it the changes the state of the mannualresetevent to Set(). But nothing is passed back to the calling code. So it must be passed as reference? Just a little unclear on how it is exactly working.

Any help would be great,

Public void Main()
	Sim S = new Sim()

	for (int i=0; i<=10; i++)

public Class Sim

	public Security[] Sec = new Security[3];
	public MannualResetEvent[] doneSec = new MannualResetEvent[3];
	public void Setup()
		for (int i=0; i<=Security.Length; i++)
			Sec[i] = new Security();
			doneSec[i] = new MannualResetEvent(false);


	public SomeBigLoop()
		for (int i=0; i<=Security.Length; i++)
			ThreadPool.QueueUserWorkItem(Security[i].SomeCalculations);	// I know I can pass an object such a mannual reset event here but in my code there would be some other object passed			



public Class Security

	private MannualResetEvent _doneEvent;
	// I know the mannualresetevent could be passed in the constructor of the class but for this example I'm not
	public void SetupResetEvent(MannualResetEvent DoneEvent)
		_doneEvent = DoneEvent;

	public void SomeCalculations()

		// do some stuff


Open in new window

Question by:mcs26
    1 Comment
    LVL 74

    Accepted Solution

    Firstly in C# when you pass a variable to function is it passed as a reference or value by default.
    Well, on a very technical level, everything is passed by value, but this technicality is not something you typically worry about. For your purposes, a value type (int, decimal, float, anything defined as struct, etc.) is passed by value, and a reference (anything defined as class is passed by reference). You can achieve pass-by-reference with value types by using out or ref modifiers.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now