无法从Backgroundworker设置statusstrip文本 [英] unable to set statusstrip text from backgroundworker

查看:70
本文介绍了无法从Backgroundworker设置statusstrip文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码

Private Sub bwrkMain_ProgressChanged(ByVal sender As System.Object, _
           ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
           Handles bwrkMain.ProgressChanged

    Dim strStatus As String = e.UserState.ToString
    Debug.Print(strStatus)
    Try
        Me.tsslStatus.Text = "status changed"
        lblStatus.Text = strStatus
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
    End Try
End Sub




但状态文本不会更改,尽管debug.print确实会打印消息.

我正在从backgroundworker线程创建的另一个线程中调用ReportProgress.

这是问题吗?

是什么原因造成的?

内联代码已更改为代码块-OriginalGriff [/edit]




but the status text does not change, debug.print does print the message though.

i am calling the ReportProgress from another thread created by the backgroundworker thread.

is this the problem?

what could be causing this?

[edit]Inline code changed to code block - OriginalGriff[/edit]

推荐答案

问题很简单:您无法访问UI组件,除非从创建它们的线程中访问-即UI线程.
当您从另一个线程尝试时,它不起作用,应该引发异常.

您需要PInvoke:
The problem is simple: you cannot access UI components except from the thread that created them - i.e. the UI thread.
When you try from another thread it doesn''t work, and should throw an exception.

You need to PInvoke it:
Private Sub ShowProgress(text As String)
	If InvokeRequired Then
		Invoke(New MethodInvoker(Function() Do
			ShowProgress(text)
		End Function))
	Else
		Me.tsslStatus.Text = text
	End If
End Sub




哎呀,我正在使用.net 2.0,但此示例不起作用"

发生这种情况时,这是个烦人! :laugh:




"hell, am using .net 2.0, and this example doesn''t work"

It''s a bugger when that happens! :laugh:

Private Delegate Sub MyDelegate(text As String)
Private Sub ShowProgress(text As String)
	If InvokeRequired Then
		BeginInvoke(New MyDelegate(AddressOf ShowProgress), text)
	Else
		tsslStatus.Text = text
	End If
End Sub


这篇关于无法从Backgroundworker设置statusstrip文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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