当从其他类(Windows Forms应用程序中Form1类之外)调用UpdateLabel函数时,Me.InvokeRequired返回false [英] Me.InvokeRequired returns false when UpdateLabel function is called from a different class (outside of the Form1 class in Windows Forms app)

查看:59
本文介绍了当从其他类(Windows Forms应用程序中Form1类之外)调用UpdateLabel函数时,Me.InvokeRequired返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在处理任务,因此我有以下代码来更新表单上的标签:

I am working with tasks, so I've got the following code to update a label on my form:

    Private Delegate Sub UpdateLabelDelegate(ByVal s As String)
    Public Sub UpdateLabel(ByVal s As String)

        If Me.InvokeRequired Then
            Me.BeginInvoke(New UpdateLabelDelegate(AddressOf UpdateLabel), New Object() {s})
            Return
        End If

        Me.lbl_Info.Text += s & vbCrLf

    End Sub

如果我在Form1类本身中调用此方法,它会很好,但是如果我尝试从处理线程(在另一个类中)更新标签,则Me.InvokeRequired返回false,并且标签不会更新:

It works fine if I call this within the Form1 class itself, but if I try to update the label from a processing thread (in a different class), then Me.InvokeRequired returns false, and the label does not get updated:

                Form1.UpdateLabel(fi.Name & ": Processed! " & Now)

我应该从其他类中调用此函数吗?

Is there a different way I should be calling this function from the other class? 

谢谢,
嘉莉

Thanks,
Carrie

推荐答案

您正在调用Form1.UpdateLabel,因此看起来好像您使用的是表单的默认实例,而不是在

You are calling Form1.UpdateLabel, so that makes it look as though you are using the default instance of the form, and not creating your own instance of form1 as in

将myForm更改为新Form1

Dim myForm as New Form1

这正确吗?


这篇关于当从其他类(Windows Forms应用程序中Form1类之外)调用UpdateLabel函数时,Me.InvokeRequired返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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