Hullo Mates,
I am looking for some resource on delegates and event raising in C# for
my threading application. Actually the whole delegation process seems very
confusing to me ... Well I am going through MSDN, still any further
resources with some examples on delegates that might help me would really be
appreciated.
http://www.wintellect.com/resources/articles.aspx?authorID=3 Jeffrey
Richter's series is posted here and it's excellent. I highly recommend his
book on the .NET Framework as well. It's not for a beginner, but it
explains a lot that you won't see much about elsewhere.
Paul Kimmel has a really good discussion/examples in his book Visual Basic
..NET Power Coding (Chapter 6) on Asynchronous Processing.
If you have any specific questions though, let me know.
I have written a simple function that has two listboxes (input and output).
Note that Function here is a type.
public delegate double Function(double arg); // create a function type
public void compute(Function f) // define a function that asks for a
function parameter
{
output.Items.Clear(); // clear the listbox
for (int i = 0; i < 25; i++)
{
numbersOut = f(numbersIn); // applay the function to each number
in the table
output.Items.Add(numbersOut.ToString()); // show the result in the
other listbox
}
}
....
private void sineMenuItem_Click(object sender, System.EventArgs e)
{
compute(new Function(Math.Sin)); // call computer with the function
Math.Sin (standard C# function)
// Since the function parameter in the call is an object, you must first
create it by using "new", and than pass it as a paramter
}
private void cosineMenuItem_Click(object sender, System.EventArgs e)
{
compute(new Function(Math.Cos)); // call computer with the function
Math.Cos (standard C# function)
}
double square(double x) // define a function square, since I cannot find
it in the libraries
{
return x * x;
}
private void squareMenuItem_Click(object sender, System.EventArgs e)
{
compute(new Function(square)); // call computer with the function square
}
private void squareRootMenuItem_Click(object sender, System.EventArgs e)
{
compute(new Function(Math.Sqrt)); // call computer with the function
Math.Sqrt (standard C# function)
}