通过委托执行可重写方法时,Invoke()和BeginInvoke()的行为有所不同

7

有人能告诉我为什么这段代码会以这种方式运行吗?请看嵌入在代码中的注释...

我是不是漏掉了什么非常明显的东西?

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;
        }
    }
}

我没有尝试过这个,也不知道有问题,但我可以看到会出现很多问题。或许有人可以解释一下 :) - leppie
3个回答

5

我还没有答案,但是我有一个我认为更清晰的程序来展示这种奇怪现象:

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");
    }
}

这不涉及任何递归调用。然而结果仍然是相同的奇特性:

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

如果您同意这是一个更简单的复现方式,请随意替换原问题中的代码,我会将其从我的答案中删除 :)

老实说,这对我来说看起来像一个错误。 我会再仔细探究一下。


看起来是BeginInvoke生成的内部代码出现了错误。查看第二个调用的堆栈跟踪,确认委托中方法信息的“正确性”(仍为MyParent.X)。 - leppie
另一个奇怪的事情是,为什么要在异步调用中使用Remoting?我真的认为它只会使用简单的线程或线程池。 - leppie
你认为远程调用会在哪里发挥作用? - Jon Skeet
在派生方法上设置一个断点。将其与普通线程池或线程调用进行比较。似乎有很多“远程处理”相关的东西。 - leppie
噢,没错。发现这个链接提到了:http://blogs.msdn.com/cbrumme/archive/2003/07/14/51495.aspx - 它还讨论了虚拟/非虚拟的东西... - Jon Skeet

1

虽然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;
        }
    }

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


0

也许不是你想要的答案,但这似乎可以解决问题:

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

或者

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接