0

I'm trying to update a label which is running in the UI thread from a Module(On a diff thread). When user hit run button a new thread is created and procedure DVD is run in it. My problem is when the program is run and the invoke required is tested it always returns false, which then doesn't update my label and also I don't get error even though the procedure is on a diff. thread. Tried a lot of stuff but nothing seems to works. What am I doing wrong? Please help..... Thanks.

Imports System.Threading

Public Class Form1

Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click

        Dim t0 As Thread

            MyPath1 = TextBox31.Text
            Label114.Text = "Working on it. Please Wait ..."

            t0 = New System.Threading.Thread(AddressOf DVD)
            t0.IsBackground = True
                t0.Start()

End sub

End Class

-----------------

Public Module MdlDVD

Sub DVD()

Do Some Work ............

If form1.Label114.InvokeRequired = True Then
            Form1.Label114.Invoke(Sub() form1.label114.Text = "Processing Complete ...")
        Else
           Form1.Label114.Text = "Processing Complete ..."
End If

End Sub

End Module

Edit:

Imports System.Threading

Public Class Form1

Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click

        Dim t0 As Thread

            MyPath1 = TextBox31.Text
            Label114.Text = "Working on it. Please Wait ..."

            t0 = New System.Threading.Thread(AddressOf DVD)
            t0.IsBackground = True
                t0.Start()

End sub


 Public Delegate Sub SetControlPropertyThreadSafeDelegate(Control_Name As Control, propertyName As String, propertyValue As Object)


 Public Sub SetControlPropertyThreadSafe(Control_Name As Control, propertyName As String, propertyValue As Object)

        If Me.Label114.InvokeRequired Then

            Me.Label114.Invoke(New SetControlPropertyThreadSafeDelegate(AddressOf SetControlPropertyThreadSafe), Control_Name, propertyName, propertyValue)

        End If

    End Sub

End Class

-----------------

Public Module

Sub DVD()

Do Some Work ............

Thread.Sleep(1000)

        form1.SetControlPropertyThreadSafe(GoProTools.Label114, "Text", "Processing Complete ...")


End Sub

End Module

But still the same problem exist. RequiredInvoke is still coming out to false. also please suggest wether or not i have implemented it correctly.. thanks ....

K_T
  • 15
  • 3
  • 1
    Possible duplicate of [How to update the GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – Joe White Jan 30 '17 at 23:33
  • thanks joe for pointing it out ... referring to question I have updated my code. – K_T Jan 31 '17 at 00:38

1 Answers1

0

A very simple fix......:-)

Imports System.Threading

Public Class Form1

Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click
            Dim t0 As Thread
            MyPath1 = TextBox31.Text
            Label114.Text = "Working on it. Please Wait ..."

        frm_ctrl=me.label114

            t0 = New System.Threading.Thread(AddressOf DVD)
            t0.IsBackground = True
            t0.Start()
End sub

 Public Sub UpdateControl(propertyValue As String)

        If frm_Ctrl.InvokeRequired Then
            frm_Ctrl.BeginInvoke(sub() Frm_Ctrl.text=PropertyValue)
        End If

 End Sub
End Class

-----------------

Public Module

public frm_ctrl as control

Sub DVD()
Do Some Work ............
Thread.Sleep(1000)
form1.UpdateControl("Processing Complete ...")

End Sub

End Module
K_T
  • 15
  • 3