如何让函数在 VB.net 中运行回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14561007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:03:44  来源:igfitidea点击:

How to have a function run a callback in VB.net

asp.netvb.netcallback

提问by Jamie Hartnoll

I'm afraid I have been Googling this, but can't find an answer that I understand, or can use.

恐怕我一直在谷歌上搜索这个,但找不到我理解或可以使用的答案。

In Javascript, you can run a function and set a callback function which it calls after the first function has run:

在 Javascript 中,您可以运行一个函数并设置一个回调函数,该函数在第一个函数运行后调用:

function doThis(callBack){
    // do things
    // do things
    if(callBack){
         callBack();
    }
}

Call this by: doThis(function () { alert("done") });

通过以下方式调用: doThis(function () { alert("done") });

So after it's finished doing things it calls an alert to tell you it's done.

因此,在它完成某件事后,它会调用警报来告诉您它已完成。

But how do you do the same server-side in VB.NET?

但是你如何在 VB.NET 中做同样的服务器端呢?

回答by sloth

Just create a method that takes an Actiondelegateas parameter:

只需创建一个将Action委托作为参数的方法:

Sub DoThis(callback as Action)

    'do this
    'do that

    If Not callback Is Nothing Then
        callback()
    End If

End Sub

and you can call it like

你可以这样称呼它

DoThis(Sub() Console.WriteLine("via callback!"))