LAMBDA多行作为主题的参数? [英] Lambda multiline as Thread parameter?

查看:112
本文介绍了LAMBDA多行作为主题的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时间前,我已经看到了多线程技术,code是这样的:

Time ago I've seen a multi-threading technique code like this:

Private Delegate Sub TextBoxUpdateUI(ByVal txt As String)

Private t As Threading.Thread = New Threading.Thread(Sub() _
        If tb2.InvokeRequired Then 
            Dim tb_delegate As New TextBoxUpdateUI(AddressOf "This Sub")
            tb2.Invoke(tb_delegate, Text)
        Else
            tb2.Text = tb1.Text
        End If _
)

的事情是,能够写一些指令线程的说法里面,我不记得很清楚,我已经看到的例子,但如果我记得很清楚它使用了 LAMBDA 就像我试图使用。

The thing is that, be able to write some instructions inside the Thread argument, I don't remember well the example that I've seen but if I remember well it used a Lambda like I've tried to use.

不过,code以上不工作,只是我想复制它,但我失去了我见过的,所以我不知道到底是怎么写的例子。

But the code above does not work, just I wanted to reproduce it, but I lost the example that I've seen so I don't know exactly how to write it.

有人可以修复的lambda?

Someone can fix the lambda?

此外,在该 AdressOf 运营商的委托行我怎么能解决它指的是?

Also, at the delegate line in the AdressOf operator how could I fix it to refer to that?

推荐答案

有两种语法风格的lambda EX pressions在VB.NET:单行和多行。当你正在做一个多行EX pression,你需要的小组功能声明。如果你不这样做,就会认为它是一个单行EX pression。所以,第一个问题是,你有子()_ 这意味着后续行符,以下如果语句是在同一行小组声明,则认为它是一个单行EX pression。

There are two syntax styles for lambda expressions in VB.NET: single-line, and multi-line. When you are doing a multi-line expression, you need to actually go to a new line immediately after the Sub or Function declaration. If you don't, it will assume that it is a single-line expression. So, the first problem is that you have a line continuation character after the Sub() _ which means that the following If statement is on the same line as the Sub declaration, so it assumes it is a single-line expression.

第二个问题是,当你正在编写一个多行的lambda EX pression,你必须有一个结束小组端功能语句。您的code丢失该行。因此,要解决您的code,只是改变这样的:

The second problem is that when you are writing a multi-line lambda expression, you must end it with an End Sub or End Function statement. Your code is missing that line. So, to fix your code, just change it like this:

Private t As Threading.Thread = New Threading.Thread(Sub()
        If tb2.InvokeRequired Then 
            Dim tb_delegate As New TextBoxUpdateUI(AddressOf "This Sub")
            tb2.Invoke(tb_delegate, Text)
        Else
            tb2.Text = tb1.Text
        End If
    End Sub
    )

这是值得一提,但是,这是一个可怕的例子。唯一的线程正在做什么,在本例中,在调用回UI线程,这是当然的,单线程的,所以额外的螺纹是完全不必要的。如果这是真的你正在尝试做的 - 调用一些在UI线程上,而不是坐等用户界面来处理请求 - 你应该只使用的BeginInvoke 而不是调用,像这样的(不需要新的线程):

It's worth mentioning, however, that this is a terrible example. The only thing that the thread is doing, in this example, is invoking back to the UI thread, which is is, of course, single-threaded, so the extra thread is completely unnecessary. If that's really all you are trying to do--invoke something on the UI thread, but not sit and wait for the UI to process the request--you should just use BeginInvoke instead of Invoke, like this (no new thread required):

If TextBox1.InvokeRequired Then
    TextBox1.BeginInvoke(Sub() TextBox1.Text = "sometext")
Else
    TextBox1.Text = "sometext"
End If

这篇关于LAMBDA多行作为主题的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆