Invoiking delegates

P

Pavils Jurjans

(I am sorry to crosspost both here and in asp.net group. I think, this
question quite certainly belongs better to this group, but by error I posted
the question to asp.net group)

Hello,

I am reading the article
http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/ , and, since
the sample code is VB.NET, I am wondering what the heck is the "Invoke"
method :) I used to think that I can simply directly call the delegate
type instance with parameters:

public class Executer
{
public delegate bool TestDelegateType(string strValue);

static bool TestDelegateMethod(string strValue)
{
Console.WriteLine("TestDelegateMethod got value:" + strValue);
return strValue == "ABC";
}

static void Main( )
{
TestDelegateType TestDelegate = new TestDelegateType(TestDelegateMethod);
// Simple call
bool result = TestDelegate("ABC");
Console.WriteLine("Call to TestDelegate returned: " + result);
Console.ReadLine();
}

}

As far as I know, I can not do anything like TestDelegate.Invoke("ABC",
null, null) in C#? So, apparently there is some syntax issue in VB.NET
preventing the direct execution of delegate, and it has to do it via Invoke
method? Please, clear this up for me, so I can have better understanding of
samples where there is no C# code.

Taking it further, what is the simplest C# syntax of launching the given
delegate asynchorously?

Thanks,

Pavils
 
J

Jon Skeet [C# MVP]

Pavils Jurjans said:
(I am sorry to crosspost both here and in asp.net group. I think, this
question quite certainly belongs better to this group, but by error I posted
the question to asp.net group)

Hello,

I am reading the article
http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/ , and, since
the sample code is VB.NET, I am wondering what the heck is the "Invoke"
method :) I used to think that I can simply directly call the delegate
type instance with parameters:

public class Executer
{
public delegate bool TestDelegateType(string strValue);

static bool TestDelegateMethod(string strValue)
{
Console.WriteLine("TestDelegateMethod got value:" + strValue);
return strValue == "ABC";
}

static void Main( )
{
TestDelegateType TestDelegate = new TestDelegateType(TestDelegateMethod);
// Simple call
bool result = TestDelegate("ABC");
Console.WriteLine("Call to TestDelegate returned: " + result);
Console.ReadLine();
}

}

As far as I know, I can not do anything like TestDelegate.Invoke("ABC",
null, null) in C#?

Well, not TestDelegate.Invoke("ABC", null, null) because it doesn't
take three arguments, but you can use TestDelegate.Invoke ("ABC") - and
indeed if you use ildasm on the above code, you'll find that's exactly
what happens.

Delegates always have three methods: Invoke (for synchronous
invocation), BeginInvoke (with parameters matching the delegate
signature parameters, followed by AsyncCallback and a state object to
be part of the IAsyncResult passed to the callback).

Again, use ildasm to look at what's in the TestDelegateType created by
the above code.
So, apparently there is some syntax issue in VB.NET
preventing the direct execution of delegate, and it has to do it via Invoke
method? Please, clear this up for me, so I can have better understanding of
samples where there is no C# code.

Taking it further, what is the simplest C# syntax of launching the given
delegate asynchorously?

Just use BeginInvoke - but remember that you ought to call EndInvoke
with the returned IAsyncResult at some stage (usually in the callback).
 
P

Pavils Jurjans

Hello, Jon,
Well, not TestDelegate.Invoke("ABC", null, null) because it doesn't
take three arguments, but you can use TestDelegate.Invoke ("ABC") - and
indeed if you use ildasm on the above code, you'll find that's exactly
what happens.

Maybe I am missing something here... if I write TestDelegate.Invoke ("ABC")
in the sample code (my post above), I get compie-time error:
error CS1533: Invoke cannot be called directly on a delegate.

Perhaps then calling delegate directly, ie
bool result = TestDelegate("ABC");
is in fact a C# syntax benefit, but upon compilation it actually is turned
into TestDelegate.Invoke("ABC"), that, in turn, is a syntax that can not be
practised in C#.

Looking for reference, I've found this link:
http://msdn2.microsoft.com/en-us/library/system.delegate_members(VS.80).aspx
But, it didn't tell anything about Invoke, BeginInvoke, etc methods. Where
should have I read this, if not the reference of the Delegate class?

Thanks,

Pavils
 
J

Jon Skeet [C# MVP]

Pavils Jurjans said:
Maybe I am missing something here... if I write TestDelegate.Invoke ("ABC")
in the sample code (my post above), I get compie-time error:
error CS1533: Invoke cannot be called directly on a delegate.

Hmm... it looks like it works in .NET 2.0 but not 1.1.
Perhaps then calling delegate directly, ie
bool result = TestDelegate("ABC");
is in fact a C# syntax benefit, but upon compilation it actually is turned
into TestDelegate.Invoke("ABC"), that, in turn, is a syntax that can not be
practised in C#.

See above.
Looking for reference, I've found this link:
http://msdn2.microsoft.com/en-us/library/system.delegate_members(VS.80).aspx
But, it didn't tell anything about Invoke, BeginInvoke, etc methods. Where
should have I read this, if not the reference of the Delegate class?

Unfortunately it's not part of the C# spec. It *is* part of the CLI
spec, however. This is from section 8.9.3 of Partition 1 - first
version:

<quote>
Delegates are the object-oriented equivalent of function pointers.
Unlike function pointers, delegates are object-oriented, type-safe, and
secure. Delegates are created by defining a class that derives from the
base type System.Delegate (see Partition IV). Each delegate type shall
provide a method named Invoke with appropriate parameters, and each
instance of a delegate forwards calls to its Invoke method to a
compatible static or instance method on a particular object. The object
and method to which it delegates are chosen when the
delegate instance is created. In addition to an instance constructor
and an Invoke method, delegates may optionally have two additional
methods: BeginInvoke and EndInvoke. These are used for asynchronous
calls.
</quote>

It's a pity you need to go to that level of detail to find the docs
though...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top