使用Begin Invoke / Invoke将节点添加到Treeview [英] Adding nodes to treeview with Begin Invoke / Invoke

查看:249
本文介绍了使用Begin Invoke / Invoke将节点添加到Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在完成我的第一个项目,并且在SO方面获得了很多宝贵的帮助,但现在我又陷入了困境。

I've been working through my first project and have had a great deal a valuable help from the guys on SO but now I'm stuck again.

下面的sub用来在添加新数据后将TreeNodes添加到TreeView中,但不包括某些文件类型/名称:

The below sub is used to add TreeNodes to a TreeView, excluding certain filetypes/names, upon addition of new data:

Sub DirSearch(ByVal strDir As String, ByVal strPattern As String, ByVal tvParent As TreeNodeCollection)
    Dim f As String
    Dim e As String
    Dim tvNode As TreeNode
    Dim ext() As String = strPattern.Split("|"c)

    Try
    For Each d In Directory.GetDirectories(strDir)
        If (UCase(IO.Path.GetFileName(d)) <> "BACKUP") And (UCase(IO.Path.GetFileName(d)) <> "BARS") Then

            tvNode = tvParent.Add(IO.Path.GetFileName(d))
            For Each e In ext
                For Each f In Directory.GetFiles(d, e)
                    If (UCase(IO.Path.GetFileName(f)) <> "DATA.XLS") And (UCase(IO.Path.GetFileName(f)) <> "SPIRIT.XLSX") Then
                        tvNode.Nodes.Add(IO.Path.GetFileName(f))

                    End If
                Next
            Next
            DirSearch(d, strPattern, tvNode.Nodes)
        End If

    Next
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try
End Sub

我现在遇到错误:


正在从错误的线程调用对此控件执行的操作。使用Control.Invoke或Control.BeginInvoke将其编组到正确的线程以执行此操作。

Action being performed on this control is being called from the wrong thread. Marshal to the correct thread using Control.Invoke or Control.BeginInvoke to perform this action.

在下一行:

tvNode = tvParent.Add(IO.Path.GetFileName(d))

很明显,我知道它与线程和 BeginInvoke / 的使用有关。调用,但是即使在阅读了有关该错误的MSDN文档之后,我也不知道从哪里开始。

Obviously, I understand its to do with 'threading' and the use of BeginInvoke / Invoke but even after reading the MSDN documentation on the error, I have no idea where to start.

仅当我添加时,才会出现此错误到初始目录的文件(这也是文件系统监视程序的主题,用于监视新添加的文件。)

This error only occurs, if I add a file to the initial directory (which is also the subject of a File System Watcher to monitor new additions).

有人愿意在我这里给我一个解释吗?

Would someone be so kind as to give me an explanation in layman's terms so I may be able to understand.

推荐答案

此代码在后台线程上运行,在该线程上修改UI是非法的元素。 Invoke / BeginInvoke 方法是安排一段代码在可修改元素的UI线程上运行的方法。例如,您可以将代码更改为以下

This code is being run on a background thread where it's illegal to modify UI elements. The Invoke / BeginInvoke methods are ways to schedule a piece of code to run on UI thread where elements can be modified. For example you could change your code to the following

Dim action As Action = Sub() tvNode.Nodes.Add(IO.Path.GetFileName(f))
tvNode.TreeView.Invoke(action)

此代码将采用名为 action 的委托实例,并在允许对 tvNode 进行编辑的UI线程上运行

This code will take the delegate instance named action and run it on the UI thread where edits to tvNode are allowed

修复先前的添加调用比较棘手,因为没有 Control 可以在其上调用 BeginInvoke 的实例。该方法的签名将需要更新,以将 Dim控件作为Control 作为参数。如果愿意,您可以为该参数传入 TreeView 。出现后,第一个 Add 可以这样更改

Fixing the earlier Add call is a bit trickier because there is no Control instance on which we can call BeginInvoke. The signature of the method will need to be updated to take a Dim control as Control as a parameter. You can pass in the TreeView for that parameter if you like. Once that is present the first Add can be changed as such

Dim outerAction As Action = Sub() tvNode = tvParent.Add(IO.Path.GetFileName(d))
control.Invoke(outerAction)

这篇关于使用Begin Invoke / Invoke将节点添加到Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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