通过委托执行可覆盖方法时,Invoke()和BeginInvoke()的行为方式不同

时间:2022-06-01 21:34:47

Can anyone tell me why this code behaves the way it does? See comments embedded in the code...

谁能告诉我为什么这段代码的行为方式呢?查看代码中嵌入的注释...

Am I missing something really obvious here?

我错过了一些非常明显的东西吗?

using System;
namespace ConsoleApplication3
{
    public class Program
    {
        static void Main(string[] args)
        {
            var c = new MyChild();
            c.X();
            Console.ReadLine();
        }
    }

    public class MyParent
    {
        public virtual void X()
        {
            Console.WriteLine("Executing MyParent");
        }
    }

    delegate void MyDelegate();

    public class MyChild : MyParent
    {
        public override void X()
        {
            Console.WriteLine("Executing MyChild");
            MyDelegate md = base.X;

            // The following two calls look like they should behave the same,
            //  but they behave differently!    

            // Why does Invoke() call the base class as expected here...
            md.Invoke();

            // ... and yet BeginInvoke() performs a recursive call within
            //  this child class and not call the base class?
            md.BeginInvoke(CallBack, null);
        }

        public void CallBack(IAsyncResult iAsyncResult)
        {
            return;
        }
    }
}

3 个解决方案

#1


5  

I don't have an answer yet, but I have what I believe to be a slightly clearer program to demonstrate the oddity:

我还没有答案,但我有一个我认为是一个更清晰的程序,以证明奇怪:

using System;

delegate void MyDelegate();

public class Program
{
    static void Main(string[] args)
    {
        var c = new MyChild();
        c.DisplayOddity();
        Console.ReadLine();
    }
}

public class MyParent
{
    public virtual void X()
    {
        Console.WriteLine("Executing MyParent.X");
    }
}

public class MyChild : MyParent
{
    public void DisplayOddity()
    {
        MyDelegate md = base.X;

        Console.WriteLine("Calling Invoke()");
        md.Invoke();                // Executes base method... fair enough

        Console.WriteLine("Calling BeginInvoke()");
        md.BeginInvoke(null, null); // Executes overridden method!
    }

    public override void X()
    {
        Console.WriteLine("Executing MyChild.X");
    }
}

This doesn't involve any recursive calls. The result is still the same oddity though:

这不涉及任何递归调用。结果仍然是同样奇怪的:

Calling Invoke()
Executing MyParent.X
Calling BeginInvoke()
Executing MyChild.X

(If you agree that this is a simpler repro, feel free to replace the code in the original question and I'll remove it from my answer :)

(如果您同意这是一个更简单的repro,请随意替换原始问题中的代码,我将从我的答案中删除它:)

To be honest, this looks like a bug to me. I'll dig around a bit more.

说实话,这对我来说就像一个错误。我会再挖掘一下。

#2


1  

While Delegate.Invoke calls the delegate method directly, Delegate.BeginInvoke internally uses ThreadPool.QueueUserWorkItem( ). md.Invoke() was only able to call base.X because a base class's methods are accessible within the derived class through the base keyword. Since the delegate started by the thread pool is external to your class, the reference to its X method is subjected to overloading, just like the code below.

Delegate.Invoke直接调用委托方法,而Delegate.BeginInvoke在内部使用ThreadPool.QueueUserWorkItem()。 md.Invoke()只能调用base.X,因为基类的方法可以通过base关键字在派生类中访问。由于线程池启动的委托是您的类的外部,因此对其X方法的引用会受到重载,就像下面的代码一样。



    public class Program
    {
        static void Main(string[] args)
        {
            MyChild a = new MyChild();
            MyDelegate ma = new MyDelegate(a.X);

            MyParent b = new MyChild();
            MyDelegate mb = new MyDelegate(b.X);

            ma.Invoke();
            mb.Invoke();
            ma.BeginInvoke(CallBack, null);
            mb.BeginInvoke(CallBack, null); //all four calls call derived MyChild.X

            Console.ReadLine();
        }

        public static void CallBack(IAsyncResult iAsyncResult)
        {
            return;
        }
    }

Debug into .NET Framework code: http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

调试.NET Framework代码:http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

#3


0  

Maybe not the answer you are looking for, but this seems to work:

也许不是你要找的答案,但这似乎有效:

ThreadPool.QueueUserWorkItem(x => md());

or

new Thread(() => md()).Start();

But you will need to do your own accounting :(

但你需要做自己的会计:(

#1


5  

I don't have an answer yet, but I have what I believe to be a slightly clearer program to demonstrate the oddity:

我还没有答案,但我有一个我认为是一个更清晰的程序,以证明奇怪:

using System;

delegate void MyDelegate();

public class Program
{
    static void Main(string[] args)
    {
        var c = new MyChild();
        c.DisplayOddity();
        Console.ReadLine();
    }
}

public class MyParent
{
    public virtual void X()
    {
        Console.WriteLine("Executing MyParent.X");
    }
}

public class MyChild : MyParent
{
    public void DisplayOddity()
    {
        MyDelegate md = base.X;

        Console.WriteLine("Calling Invoke()");
        md.Invoke();                // Executes base method... fair enough

        Console.WriteLine("Calling BeginInvoke()");
        md.BeginInvoke(null, null); // Executes overridden method!
    }

    public override void X()
    {
        Console.WriteLine("Executing MyChild.X");
    }
}

This doesn't involve any recursive calls. The result is still the same oddity though:

这不涉及任何递归调用。结果仍然是同样奇怪的:

Calling Invoke()
Executing MyParent.X
Calling BeginInvoke()
Executing MyChild.X

(If you agree that this is a simpler repro, feel free to replace the code in the original question and I'll remove it from my answer :)

(如果您同意这是一个更简单的repro,请随意替换原始问题中的代码,我将从我的答案中删除它:)

To be honest, this looks like a bug to me. I'll dig around a bit more.

说实话,这对我来说就像一个错误。我会再挖掘一下。

#2


1  

While Delegate.Invoke calls the delegate method directly, Delegate.BeginInvoke internally uses ThreadPool.QueueUserWorkItem( ). md.Invoke() was only able to call base.X because a base class's methods are accessible within the derived class through the base keyword. Since the delegate started by the thread pool is external to your class, the reference to its X method is subjected to overloading, just like the code below.

Delegate.Invoke直接调用委托方法,而Delegate.BeginInvoke在内部使用ThreadPool.QueueUserWorkItem()。 md.Invoke()只能调用base.X,因为基类的方法可以通过base关键字在派生类中访问。由于线程池启动的委托是您的类的外部,因此对其X方法的引用会受到重载,就像下面的代码一样。



    public class Program
    {
        static void Main(string[] args)
        {
            MyChild a = new MyChild();
            MyDelegate ma = new MyDelegate(a.X);

            MyParent b = new MyChild();
            MyDelegate mb = new MyDelegate(b.X);

            ma.Invoke();
            mb.Invoke();
            ma.BeginInvoke(CallBack, null);
            mb.BeginInvoke(CallBack, null); //all four calls call derived MyChild.X

            Console.ReadLine();
        }

        public static void CallBack(IAsyncResult iAsyncResult)
        {
            return;
        }
    }

Debug into .NET Framework code: http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

调试.NET Framework代码:http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

#3


0  

Maybe not the answer you are looking for, but this seems to work:

也许不是你要找的答案,但这似乎有效:

ThreadPool.QueueUserWorkItem(x => md());

or

new Thread(() => md()).Start();

But you will need to do your own accounting :(

但你需要做自己的会计:(