Question Updating UI label from another class in another thread?

Status
Not open for further replies.

tommykent1210

Member
Joined
Nov 2, 2011
Messages
10
Programming Experience
1-3
Hey guys (and girls?),

I've been programming in vb.NET for a while, and I've just began to sink my teeth into threading. And to be honest, its turning out to be a real pain.

Here is the code:

Form1.vb:
Imports System.Threading
Public Class Form1
    Dim trd As Thread
    Public Shared classy As New Class1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        trd = New Thread(AddressOf Dostuff)
        trd.IsBackground = True
        trd.Start()
    End Sub

    Public Delegate Sub SetTextDelegate(ByVal TheText As String)
    Private Sub delSetText(ByVal TheText As String)
        Label1.Text = TheText
        Label1.Update()
    End Sub

    Public Sub ChangeText(ByVal TheText As String)
        If Label1.InvokeRequired Then
            BeginInvoke(New SetTextDelegate(AddressOf delSetText), TheText)
        Else
            delSetText(TheText)
        End If
    End Sub


    Sub Dostuff()
        classy.Main()
    End Sub

End Class


Class1.vb:
Imports System.Threading
Public Class Class1


    Public Sub Main()
        For i As Integer = 0 To 100
            Form1.ChangeText("button " & i)
        Next
    End Sub

End Class



So, I have a form (form1) with a label on, this label simply reports the status of the thread. The thread begins the Class1.Main() sub, and I have a delegate setup to change the text propoerty of the label.

Now, I know the label is created on the UI thread, so I assumed the delegate would work. However, it seems that it updates the .text property, but doesnt redraw the control when .Update() is called :(

What am I doing wrong?

I have found lots of examples of delegates, but all run from within the same class. I want to use threads too, not a backgroundworker! This code is a "simplified" version of a project I am on, which is encountering the same problems :(

Thanks,
Tom
 
Don't overcomplicate, events is such a basic thing. To enable a class to give notifications you just have to declare an event in it, whenever the notification is to be given you raise that event.

So, I create the event in class1.vb, and raise it in form1? How does the event update the main form? And how does the event sub know what the data is?

Also, what is the event ACTUALLY doing? I can't even figure out why calling the event will help :( I just dont understand threading or events much.


Could you write me an example, I prefer to see code than try blindly stabbing in the dark until I find how to update the controls from another class :(
 
Last edited:
So, i create the event in class1.vb, and raise it in form1?
Only the class that declares an event can raise it.
How does the event update the main form?
The event doesn't do anything but provide notification. By handling the event the subscriber will get notificed and can act upon that.
And how does the event sub know what the data is?
Event notification can carry event data. It's about time you read the article I linked to in post 4.
 
Here is an example that illustrates what you need:

frmMain form:
VB.NET:
Private WithEvents SQLMethods As New HLSoft.HLSoftSQLLib

    Private Sub SQLProgress(ByVal strMessage As String, ByVal ProgressPercentage As Integer) Handles SQLMethods.evPercentCompleted
        UpdateProgressBar(strMessage, ProgressPercentage, 100)
    End Sub

SomeClassThatNeedsToSignal:
VB.NET:
    Public Event evPercentCompleted(ByVal strMessage As String, ByVal percent As Integer)

    Private Sub AsyncRestoreProgress(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
        RaiseEvent evPercentCompleted("Restoring database '" & tmpDatabase & "'... ", e.ProgressPercentage)
    End Sub
 
i have some problem like you

Hey guys (and girls?),

I've been programming in vb.NET for a while, and I've just began to sink my teeth into threading. And to be honest, its turning out to be a real pain.

Here is the code:

Form1.vb:
Imports System.Threading
Public Class Form1
    Dim trd As Thread
    Public Shared classy As New Class1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        trd = New Thread(AddressOf Dostuff)
        trd.IsBackground = True
        trd.Start()
    End Sub

    Public Delegate Sub SetTextDelegate(ByVal TheText As String)
    Private Sub delSetText(ByVal TheText As String)
        Label1.Text = TheText
        Label1.Update()
    End Sub

    Public Sub ChangeText(ByVal TheText As String)
        If Label1.InvokeRequired Then
            BeginInvoke(New SetTextDelegate(AddressOf delSetText), TheText)
        Else
            delSetText(TheText)
        End If
    End Sub


    Sub Dostuff()
        classy.Main()
    End Sub

End Class


Class1.vb:
Imports System.Threading
Public Class Class1


    Public Sub Main()
        For i As Integer = 0 To 100
            Form1.ChangeText("button " & i)
        Next
    End Sub

End Class



So, I have a form (form1) with a label on, this label simply reports the status of the thread. The thread begins the Class1.Main() sub, and I have a delegate setup to change the text propoerty of the label.

Now, I know the label is created on the UI thread, so I assumed the delegate would work. However, it seems that it updates the .text property, but doesnt redraw the control when .Update() is called :(

What am I doing wrong?

I have found lots of examples of delegates, but all run from within the same class. I want to use threads too, not a backgroundworker! This code is a "simplified" version of a project I am on, which is encountering the same problems :(

Thanks,
Tom

hi i have some problem like this but i can't resolve the problem can you help me i really need some help
 
hi i have some problem like this but i can't resolve the problem can you help me i really need some help

This thread already contains a solution to the problem specified. If that solution doesn't help you then your problem is presumably different in some specific way. Rather than try to resurrect a thread that is almost 7 years old, please create a new thread of your own that details the specifics of YOUR problem.
 
Status
Not open for further replies.
Back
Top