C#中的回调,调用顺序和返回

发布于 2024-11-06 23:22:53 字数 297 浏览 1 评论 0原文

一个关于回调的简单问题。回调函数完成后是否返回到调用函数的下一行?

class A
{
 public delegate void A();
 public event A onA;

 public void func()
 {
   //some code 1
  onA();
  //some code 2 
 }

所以问题是 onA 事件是否会执行相应的处理程序,然后返回到“某些代码 2”位,或者这是异步的并且代码不会等待事件被完全处理?

我希望问题很清楚。

谢谢 }

A simple question on callbacks. Do callback functions return to the next line in the calling function after completion ?

class A
{
 public delegate void A();
 public event A onA;

 public void func()
 {
   //some code 1
  onA();
  //some code 2 
 }

So the question is will onA event go and execute the respective handler and then come back to 'some code 2' bit or is this asynchronous and code will not wait for the event to be fully handled?

I hope the question is clear.

Thanks
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

灵芸 2024-11-13 23:22:53

您使用 delegate: 的方式是同步的。如果您想要异步,则必须使用以下方法调用委托:BeginInvoke 方法。

The way you used delegate: is synchronous. If you want asynchronous you must invoke delegate with: BeginInvoke method.

<逆流佳人身旁 2024-11-13 23:22:53

是的,在您的示例中, onA() 将触发所有连接到 A 的事件处理程序以触发。它们只是将被调用的方法。全部调用后,控制权将返回到 func()。

它不是异步的——您只使用一个线程。一切都会按照明确的顺序发生。

一个好的实验方法是使用内置调试器逐步执行示例中的代码。

Yes, in your example onA() will trigger all over the event handlers hooked up to A to fire. They are just methods that will be called. After they are all called, control will return to func().

It is not asynchronous - you are only using one thread. Everything will happen in a well defined order.

A good way to experiment would be to step through the code in your example using the built in debugger.

秋日私语 2024-11-13 23:22:53

您的代码不是异步的。但是您可以异步使用委托

Your code isn't assync. But you can Use Delegates Asynchronously.

温柔戏命师 2024-11-13 23:22:53

不,调用事件不是异步的事情。您的代码 func() 仅在 onA() 结束运行后才会继续。

如果需要运行异步代码,您可以使用 BeginInvokeThreading

了解有关委托调用的更多信息此处。

No, calling a event isn't a assync thing. Your code func() will only continue after onA() ends running.

You would use BeginInvoke or Threading if will wants assync code running.

Read more about delegate invokes here.

冰雪之触 2024-11-13 23:22:53

正如其他人指出的那样,这完全是同步的。如果您想异步执行此操作,则必须以不同的方式编写此操作。

此外,如果未订阅事件“onA”,onA() 将引发空引用异常。

通常的模式是定义一个事件“Foo”和一个在事件发生时调用的方法“OnFoo”。从事件的名称来看,我怀疑这就是您想要的 - 例如:-

class Foo // Class and member names must be distinct
{
    public delegate void ADelegate();
    public event ADelegate A;

    private void OnA()
    {
        if(A != null)
            A();
    }

    public void Func()
    {
        // Some code...
        OnA();
        // More code...
    }
}

如果您想异步调用订阅的事件处理程序,您可以使用 BeginInvoke() 和 EndInvoke() 因此:-

class Foo // Class and member names must be distinct
{
    public delegate void ADelegate();
    public event ADelegate A;

    private void OnA()
    {
        if (A == null) return;

        // There may be multiple subscribers, invoke each separately.
        foreach(ADelegate del in A.GetInvocationList())
            del.BeginInvoke(SubscriberCallback, del);
    }

    private void SubscriberCallback(IAsyncResult result)
    {
        var del = (ADelegate) result.AsyncState;
        del.EndInvoke(result);
        // Do something in the callback...
    }

    public void Func()
    {
        // Some code...
        OnA();
        // More code...
    }
}

请注意,此代码不会等待完成执行事件订阅者,您必须通过事件调用线程化异步结果以确保发生这种情况。

请注意,“回调”是您在异步 BeginInvoke 中指定的方法(因为一旦异步工作完成,它就会“回调”),并且不会返回到 Func(),因为它是在单独的线程中执行的。

As others have pointed out, this is entirely synchronous. If you wanted to execute this asynchronously you would have to write this differently.

Additionally, if the event 'onA' is not subscribed to, onA() will raise a null reference exception.

The usual pattern is to define an event 'Foo' and a method 'OnFoo' which you call when the event occurs. From the name of the event I suspect this is what you desire - e.g.:-

class Foo // Class and member names must be distinct
{
    public delegate void ADelegate();
    public event ADelegate A;

    private void OnA()
    {
        if(A != null)
            A();
    }

    public void Func()
    {
        // Some code...
        OnA();
        // More code...
    }
}

If you want to call the subscribed event handlers asynchronously you can use BeginInvoke() and EndInvoke() thus:-

class Foo // Class and member names must be distinct
{
    public delegate void ADelegate();
    public event ADelegate A;

    private void OnA()
    {
        if (A == null) return;

        // There may be multiple subscribers, invoke each separately.
        foreach(ADelegate del in A.GetInvocationList())
            del.BeginInvoke(SubscriberCallback, del);
    }

    private void SubscriberCallback(IAsyncResult result)
    {
        var del = (ADelegate) result.AsyncState;
        del.EndInvoke(result);
        // Do something in the callback...
    }

    public void Func()
    {
        // Some code...
        OnA();
        // More code...
    }
}

Note that this code won't wait to finish executing the event subscriber(s), you would have to thread the async result through the event call to ensure this happens.

Note that the 'callback' is the method you specify in the asynchronous BeginInvoke (since it is 'called back' once the async work is done), and doesn't return to Func() as it is executed in a separate thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文