vb.net - 如何使用参数将新任务声明为 SUB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16119864/
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 13:20:35  来源:igfitidea点击:

vb.net - How to Declare new task as SUB with parameters

vb.netparameterstaskdeclare

提问by newbeee

As you know we have a new syntax in vb.net with possibility to create inline tasks so we could run it asynchronously.

如您所知,我们在 vb.net 中有一个新语法,可以创建内联任务,以便我们可以异步运行它。

This is the correct code:

这是正确的代码:

        Dim testDeclaring As New Task(Sub()

                                      End Sub)
        testDeclaring.Start()

but now I need to pass a parameter in the subroutine and I can't find correct syntax for that. Is it possible any way?

但现在我需要在子例程中传递一个参数,但我找不到正确的语法。有可能吗?

回答by dbasnett

If you want to pass a parameter you could do this

如果你想传递一个参数,你可以这样做

    Dim someAction As Action(Of Object) = Sub(s As Object)
                                              Debug.WriteLine(DirectCast(s, String))
                                          End Sub

    Dim testDeclaring As New Task(someAction, "tryme")
    testDeclaring.Start()

回答by Kenneth

It's not possible. However, you could just use the parameters from the current scope:

这是不可能的。但是,您可以只使用当前作用域中的参数:

Public Function SomeFunction()

    Dim somevariable as Integer = 5

    Dim testDeclaring As New Task(Sub()
                                   Dim sum as integer = somevariable + 1  ' No problems here, sum will be 6
                              End Sub)
    testDeclaring.Start()

End Function   

回答by Jorge Solano

Dont know if you looking for this:

不知道你是否在寻找这个:

Dim t As Task = New Task(Sub() RemoveBreakPages(doc))

Sub RemoveBreakPages(ByRef doc As Document)
        Dim paragraphs As NodeCollection = doc.GetChildNodes(NodeType.Paragraph, True)
        Dim runs As NodeCollection = doc.GetChildNodes(NodeType.Run, True)
        For Each p In paragraphs
            If CType(p, Paragraph).ParagraphFormat().PageBreakBefore() Then
                CType(p, Paragraph).ParagraphFormat().PageBreakBefore = False
            End If
        Next
    End Sub

Regards.

问候。