Another Cross Threading - update object question!

Posted on 2008-10-20
Last Modified: 2012-05-05
I found this in the EE Kb:
You have to use a delegate and call the forms invoke method as follows:

Private Delegate Sub delEditLabel()

Private sub EditLabel()
       'put your code to edit the controls here
End Sub

Then call the following from your thread

Me.Invoke(New delEditLabel(AddressOf Me.EditLabel), New Object())

'FROM CanvasYou:
'Shouldn't there be an " End of "
'for the Private Delegate Sub delEdit Label?

'I have looked at this example -
'But I am still having issues


I still can not get it to work - I attached the form -
I would appreciate it if I could get some help on this one.
Thanks in Advanced...

I am new to VB.NET
so if this looks really bad - and you have better way - PLEASE let me know.
Thanks again!
Imports System.IO

Imports System.Threading

Public Class frmPhotoCopy

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        lblCopyInProgress.Visible = True

        'Trying to use a Thread here!

        'But it messes up because the objects

        'are on the form - and they do not cross thread properly

        Dim T As Thread

        T = New Thread(AddressOf PhotoGroup01)


    End Sub

    Private Sub frmPhotoCopy_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Get the Paths for the photo copy from

        'stored XML

        'Calling a function on frmMain to retreive the XML Values

        lblPhotoCopyFrom.Text = frmMain.CallXML("ServerPhotoDirXML")

        lblPhotoCopyTo.Text = frmMain.CallXML("clientPhotoDirXML")

        'Verbose logger on frmMain Function

        frmMain.Verbose("Photo Paths Loaded from XML...")

    End Sub

    Private Sub UpdateLabel(ByVal LabelToUpdate As Label, ByVal TextToUse As String)

        'This is where I tried to make a sub that would update the label

        'but the invoke did not properly call it!

    End Sub

    Private Sub UpdatePB(ByVal ProgressBar As ProgressBar, ByVal PBMin As Integer, ByVal PBMax As Integer, ByVal PBStep As Integer, ByVal PBValue As Integer)

        'This sub is used to update the progress bar

    End Sub

    Public Sub UpdateFolder(ByVal source As String, ByVal dest As String, ByVal label As Label, ByVal pb As ProgressBar)

        Dim sDir As New DirectoryInfo(source)

        If Microsoft.VisualBasic.Right(dest, 1).CompareTo("\") <> 0 Then

            dest = dest & "\"

        End If

        label.Text = "Please wait while BQC gets folder info..."


            Dim files As FileInfo() = sDir.GetFiles()


            'This is where the progressbar should be called or invoked

            'to update the progress bar


            pb.Minimum = 0

            pb.Maximum = files.Length

            pb.Value = 0

            pb.Step = 1

            Dim sFile As FileInfo

            For Each sFile In sDir.GetFiles()


                    sFile.CopyTo(dest & sFile.Name)


                    ' Me.Invoke(New delUpdateLabel(AddressOf Me.UpdateLabel), New Object())

                    ' This is the issue - I can not update anything on the form


                    label.Text = "Copying: " & vbCrLf & sFile.FullName



                Catch ex As IOException

                    If sFile.LastWriteTime > File.GetLastWriteTime(dest & sFile.Name) Then

                        sFile.CopyTo(dest & sFile.Name, True)


                        'Invoke label should be here:

                        'Unable to update the form form thread

                        'The refresh does not work either

                        label.Text = "Newer File: " & vbCrLf & sFile.FullName



                    End If

                Catch ex As Exception

                    MsgBox("Folder Copy Error #75" & vbCrLf & Err.Description)

                    label.Text = "Error: #75"

                    frmMain.Verbose("Error: #75     " & Err.Description)


                End Try




            label.Text = "Copy of: " & source & " to: " & dest & vbCrLf & "Complete!"


            MsgBox("Folder Copy Error #76" & vbCrLf & Err.Description)

            label.Text = "Error: #76"

            frmMain.Verbose("Error: #76      " & Err.Description)

        End Try

    End Sub

    Sub PhotoGroup01()

        '64 folders - Copy

        CopyPhotos("00", Label1, ProgressBar1)

        CopyPhotos("01", Label1, ProgressBar1)

        CopyPhotos("02", Label1, ProgressBar1)

        CopyPhotos("03", Label1, ProgressBar1)

        CopyPhotos("04", Label1, ProgressBar1)

        CopyPhotos("05", Label1, ProgressBar1)

        CopyPhotos("06", Label1, ProgressBar1)

        CopyPhotos("07", Label1, ProgressBar1)

        CopyPhotos("08", Label1, ProgressBar1)

        CopyPhotos("09", Label1, ProgressBar1)

        CopyPhotos("10", Label1, ProgressBar1)

        CopyPhotos("11", Label1, ProgressBar1)

        CopyPhotos("12", Label1, ProgressBar1)

        CopyPhotos("13", Label1, ProgressBar1)

        CopyPhotos("14", Label1, ProgressBar1)

        CopyPhotos("15", Label1, ProgressBar1)

        CopyPhotos("16", Label1, ProgressBar1)

        CopyPhotos("17", Label1, ProgressBar1)

        CopyPhotos("18", Label1, ProgressBar1)

        CopyPhotos("19", Label1, ProgressBar1)

        CopyPhotos("20", Label1, ProgressBar1)

        CopyPhotos("21", Label1, ProgressBar1)

        CopyPhotos("22", Label1, ProgressBar1)

        CopyPhotos("23", Label1, ProgressBar1)

        CopyPhotos("24", Label1, ProgressBar1)

        CopyPhotos("25", Label1, ProgressBar1)

        CopyPhotos("26", Label1, ProgressBar1)

        CopyPhotos("27", Label1, ProgressBar1)

        CopyPhotos("28", Label1, ProgressBar1)

        CopyPhotos("29", Label1, ProgressBar1)

        CopyPhotos("30", Label1, ProgressBar1)

        CopyPhotos("31", Label1, ProgressBar1)

        CopyPhotos("32", Label1, ProgressBar1)

        CopyPhotos("33", Label1, ProgressBar1)

        CopyPhotos("34", Label1, ProgressBar1)

        CopyPhotos("35", Label1, ProgressBar1)

        CopyPhotos("36", Label1, ProgressBar1)

        CopyPhotos("37", Label1, ProgressBar1)

        CopyPhotos("38", Label1, ProgressBar1)

        CopyPhotos("39", Label1, ProgressBar1)

        CopyPhotos("40", Label1, ProgressBar1)

        CopyPhotos("41", Label1, ProgressBar1)

        CopyPhotos("42", Label1, ProgressBar1)

        CopyPhotos("43", Label1, ProgressBar1)

        CopyPhotos("44", Label1, ProgressBar1)

        CopyPhotos("45", Label1, ProgressBar1)

        CopyPhotos("46", Label1, ProgressBar1)

        CopyPhotos("47", Label1, ProgressBar1)

        CopyPhotos("48", Label1, ProgressBar1)

        CopyPhotos("49", Label1, ProgressBar1)

        CopyPhotos("50", Label1, ProgressBar1)

        CopyPhotos("51", Label1, ProgressBar1)

        CopyPhotos("52", Label1, ProgressBar1)

        CopyPhotos("53", Label1, ProgressBar1)

        CopyPhotos("54", Label1, ProgressBar1)

        CopyPhotos("55", Label1, ProgressBar1)

        CopyPhotos("56", Label1, ProgressBar1)

        CopyPhotos("57", Label1, ProgressBar1)

        CopyPhotos("58", Label1, ProgressBar1)

        CopyPhotos("59", Label1, ProgressBar1)

        CopyPhotos("60", Label1, ProgressBar1)

        CopyPhotos("61", Label1, ProgressBar1)

        CopyPhotos("62", Label1, ProgressBar1)

        CopyPhotos("63", Label1, ProgressBar1)

    End Sub

    Sub PhotoGroup02()

        '64 folders - Copy

        'CopyPhotos("00", Label1, ProgressBar1)

        CopyPhotos("64", Label2, ProgressBar2)

        CopyPhotos("65", Label2, ProgressBar2)

        CopyPhotos("66", Label2, ProgressBar2)

        CopyPhotos("67", Label2, ProgressBar2)

        CopyPhotos("68", Label2, ProgressBar2)

        CopyPhotos("69", Label2, ProgressBar2)

        CopyPhotos("70", Label2, ProgressBar2)

        CopyPhotos("71", Label2, ProgressBar2)

        CopyPhotos("72", Label2, ProgressBar2)

        CopyPhotos("73", Label2, ProgressBar2)

        CopyPhotos("74", Label2, ProgressBar2)

        CopyPhotos("75", Label2, ProgressBar2)

        CopyPhotos("76", Label2, ProgressBar2)

        CopyPhotos("77", Label2, ProgressBar2)

        CopyPhotos("78", Label2, ProgressBar2)

        CopyPhotos("79", Label2, ProgressBar2)

        CopyPhotos("80", Label2, ProgressBar2)

        CopyPhotos("81", Label2, ProgressBar2)

        CopyPhotos("82", Label2, ProgressBar2)

        CopyPhotos("83", Label2, ProgressBar2)

        CopyPhotos("84", Label2, ProgressBar2)

        CopyPhotos("85", Label2, ProgressBar2)

        CopyPhotos("86", Label2, ProgressBar2)

        CopyPhotos("87", Label2, ProgressBar2)

        CopyPhotos("88", Label2, ProgressBar2)

        CopyPhotos("89", Label2, ProgressBar2)

        CopyPhotos("90", Label2, ProgressBar2)

        CopyPhotos("91", Label2, ProgressBar2)

        CopyPhotos("92", Label2, ProgressBar2)

        CopyPhotos("93", Label2, ProgressBar2)

        CopyPhotos("94", Label2, ProgressBar2)

        CopyPhotos("95", Label2, ProgressBar2)

        CopyPhotos("96", Label2, ProgressBar2)

        CopyPhotos("97", Label2, ProgressBar2)

        CopyPhotos("98", Label2, ProgressBar2)

        CopyPhotos("99", Label2, ProgressBar2)

        CopyPhotos("0A", Label2, ProgressBar2)

        CopyPhotos("0B", Label2, ProgressBar2)

        CopyPhotos("0C", Label2, ProgressBar2)

        CopyPhotos("0D", Label2, ProgressBar2)

        CopyPhotos("0E", Label2, ProgressBar2)

        CopyPhotos("0F", Label2, ProgressBar2)

        CopyPhotos("1A", Label2, ProgressBar2)

        CopyPhotos("1B", Label2, ProgressBar2)

        CopyPhotos("1C", Label2, ProgressBar2)

        CopyPhotos("1D", Label2, ProgressBar2)

        CopyPhotos("1E", Label2, ProgressBar2)

        CopyPhotos("1F", Label2, ProgressBar2)

        CopyPhotos("2A", Label2, ProgressBar2)

        CopyPhotos("2B", Label2, ProgressBar2)

        CopyPhotos("2C", Label2, ProgressBar2)

        CopyPhotos("2D", Label2, ProgressBar2)

        CopyPhotos("2E", Label2, ProgressBar2)

        CopyPhotos("2F", Label2, ProgressBar2)

        CopyPhotos("3A", Label2, ProgressBar2)

        CopyPhotos("3B", Label2, ProgressBar2)

        CopyPhotos("3C", Label2, ProgressBar2)

        CopyPhotos("3D", Label2, ProgressBar2)

        CopyPhotos("3E", Label2, ProgressBar2)

        CopyPhotos("3F", Label2, ProgressBar2)

        CopyPhotos("4A", Label2, ProgressBar2)

        CopyPhotos("4B", Label2, ProgressBar2)

        CopyPhotos("4C", Label2, ProgressBar2)

        CopyPhotos("4D", Label2, ProgressBar2)

    End Sub

    Sub PhotoGroup03()

        '64 folders - Copy

        'CopyPhotos("00", Label1, ProgressBar1)

        CopyPhotos("4E", Label3, ProgressBar3)

        CopyPhotos("4F", Label3, ProgressBar3)

        CopyPhotos("5A", Label3, ProgressBar3)

        CopyPhotos("5B", Label3, ProgressBar3)

        CopyPhotos("5C", Label3, ProgressBar3)

        CopyPhotos("5D", Label3, ProgressBar3)

        CopyPhotos("5E", Label3, ProgressBar3)

        CopyPhotos("5F", Label3, ProgressBar3)

        CopyPhotos("6A", Label3, ProgressBar3)

        CopyPhotos("6B", Label3, ProgressBar3)

        CopyPhotos("6C", Label3, ProgressBar3)

        CopyPhotos("6D", Label3, ProgressBar3)

        CopyPhotos("6E", Label3, ProgressBar3)

        CopyPhotos("6F", Label3, ProgressBar3)

        CopyPhotos("7A", Label3, ProgressBar3)

        CopyPhotos("7B", Label3, ProgressBar3)

        CopyPhotos("7C", Label3, ProgressBar3)

        CopyPhotos("7D", Label3, ProgressBar3)

        CopyPhotos("7E", Label3, ProgressBar3)

        CopyPhotos("7F", Label3, ProgressBar3)

        CopyPhotos("8A", Label3, ProgressBar3)

        CopyPhotos("8B", Label3, ProgressBar3)

        CopyPhotos("8C", Label3, ProgressBar3)

        CopyPhotos("8D", Label3, ProgressBar3)

        CopyPhotos("8E", Label3, ProgressBar3)

        CopyPhotos("8F", Label3, ProgressBar3)

        CopyPhotos("9A", Label3, ProgressBar3)

        CopyPhotos("9B", Label3, ProgressBar3)

        CopyPhotos("9C", Label3, ProgressBar3)

        CopyPhotos("9D", Label3, ProgressBar3)

        CopyPhotos("9E", Label3, ProgressBar3)

        CopyPhotos("9F", Label3, ProgressBar3)

        CopyPhotos("A0", Label3, ProgressBar3)

        CopyPhotos("A1", Label3, ProgressBar3)

        CopyPhotos("A2", Label3, ProgressBar3)

        CopyPhotos("A3", Label3, ProgressBar3)

        CopyPhotos("A4", Label3, ProgressBar3)

        CopyPhotos("A5", Label3, ProgressBar3)

        CopyPhotos("A6", Label3, ProgressBar3)

        CopyPhotos("A7", Label3, ProgressBar3)

        CopyPhotos("A8", Label3, ProgressBar3)

        CopyPhotos("A9", Label3, ProgressBar3)

        CopyPhotos("B0", Label3, ProgressBar3)

        CopyPhotos("B1", Label3, ProgressBar3)

        CopyPhotos("B2", Label3, ProgressBar3)

        CopyPhotos("B3", Label3, ProgressBar3)

        CopyPhotos("B4", Label3, ProgressBar3)

        CopyPhotos("B5", Label3, ProgressBar3)

        CopyPhotos("B6", Label3, ProgressBar3)

        CopyPhotos("B7", Label3, ProgressBar3)

        CopyPhotos("B8", Label3, ProgressBar3)

        CopyPhotos("B9", Label3, ProgressBar3)

        CopyPhotos("C0", Label3, ProgressBar3)

        CopyPhotos("C1", Label3, ProgressBar3)

        CopyPhotos("C2", Label3, ProgressBar3)

        CopyPhotos("C3", Label3, ProgressBar3)

        CopyPhotos("C4", Label3, ProgressBar3)

        CopyPhotos("C5", Label3, ProgressBar3)

        CopyPhotos("C6", Label3, ProgressBar3)

        CopyPhotos("C7", Label3, ProgressBar3)

        CopyPhotos("C8", Label3, ProgressBar3)

        CopyPhotos("C9", Label3, ProgressBar3)

        CopyPhotos("D0", Label3, ProgressBar3)

        CopyPhotos("D1", Label3, ProgressBar3)

    End Sub

    Sub PhotoGroup04()

        '64 folders - Copy

        'CopyPhotos("00", Label1, ProgressBar1)

        CopyPhotos("D2", Label4, ProgressBar4)

        CopyPhotos("D3", Label4, ProgressBar4)

        CopyPhotos("D4", Label4, ProgressBar4)

        CopyPhotos("D5", Label4, ProgressBar4)

        CopyPhotos("D6", Label4, ProgressBar4)

        CopyPhotos("D7", Label4, ProgressBar4)

        CopyPhotos("D8", Label4, ProgressBar4)

        CopyPhotos("D9", Label4, ProgressBar4)

        CopyPhotos("E0", Label4, ProgressBar4)

        CopyPhotos("E1", Label4, ProgressBar4)

        CopyPhotos("E2", Label4, ProgressBar4)

        CopyPhotos("E3", Label4, ProgressBar4)

        CopyPhotos("E4", Label4, ProgressBar4)

        CopyPhotos("E5", Label4, ProgressBar4)

        CopyPhotos("E6", Label4, ProgressBar4)

        CopyPhotos("E7", Label4, ProgressBar4)

        CopyPhotos("E8", Label4, ProgressBar4)

        CopyPhotos("E9", Label4, ProgressBar4)

        CopyPhotos("F0", Label4, ProgressBar4)

        CopyPhotos("F1", Label4, ProgressBar4)

        CopyPhotos("F2", Label4, ProgressBar4)

        CopyPhotos("F3", Label4, ProgressBar4)

        CopyPhotos("F4", Label4, ProgressBar4)

        CopyPhotos("F5", Label4, ProgressBar4)

        CopyPhotos("F6", Label4, ProgressBar4)

        CopyPhotos("F7", Label4, ProgressBar4)

        CopyPhotos("F8", Label4, ProgressBar4)

        CopyPhotos("F9", Label4, ProgressBar4)

        CopyPhotos("AA", Label4, ProgressBar4)

        CopyPhotos("AB", Label4, ProgressBar4)

        CopyPhotos("AC", Label4, ProgressBar4)

        CopyPhotos("AD", Label4, ProgressBar4)

        CopyPhotos("AE", Label4, ProgressBar4)

        CopyPhotos("AF", Label4, ProgressBar4)

        CopyPhotos("BA", Label4, ProgressBar4)

        CopyPhotos("BB", Label4, ProgressBar4)

        CopyPhotos("BC", Label4, ProgressBar4)

        CopyPhotos("BD", Label4, ProgressBar4)

        CopyPhotos("BE", Label4, ProgressBar4)

        CopyPhotos("BF", Label4, ProgressBar4)

        CopyPhotos("CA", Label4, ProgressBar4)

        CopyPhotos("CB", Label4, ProgressBar4)

        CopyPhotos("CC", Label4, ProgressBar4)

        CopyPhotos("CD", Label4, ProgressBar4)

        CopyPhotos("CE", Label4, ProgressBar4)

        CopyPhotos("CF", Label4, ProgressBar4)

        CopyPhotos("DA", Label4, ProgressBar4)

        CopyPhotos("DB", Label4, ProgressBar4)

        CopyPhotos("DC", Label4, ProgressBar4)

        CopyPhotos("DD", Label4, ProgressBar4)

        CopyPhotos("DE", Label4, ProgressBar4)

        CopyPhotos("DF", Label4, ProgressBar4)

        CopyPhotos("EA", Label4, ProgressBar4)

        CopyPhotos("EB", Label4, ProgressBar4)

        CopyPhotos("EC", Label4, ProgressBar4)

        CopyPhotos("ED", Label4, ProgressBar4)

        CopyPhotos("EE", Label4, ProgressBar4)

        CopyPhotos("EF", Label4, ProgressBar4)

        CopyPhotos("FA", Label4, ProgressBar4)

        CopyPhotos("FB", Label4, ProgressBar4)

        CopyPhotos("FC", Label4, ProgressBar4)

        CopyPhotos("FD", Label4, ProgressBar4)

        CopyPhotos("FE", Label4, ProgressBar4)

        CopyPhotos("FF", Label4, ProgressBar4)

    End Sub

    Private Sub CopyPhotos(ByVal folder As String, ByVal label As Label, ByVal progressbar As ProgressBar)

        'This sub just cleans up the paths by adding folders to them

        'It also lets me pause to see what is going on

        Dim fromPath As String = lblPhotoCopyFrom.Text

        Dim toPath As String = lblPhotoCopyTo.Text

        'From and to paths do not have the "\" in them:

        fromPath = fromPath & "\" & folder

        toPath = toPath & "\" & folder

        'This message Box only here as a Pause - and to debug the paths to see the actual output

        'it will be removed

        MsgBox(fromPath & vbCrLf & toPath & vbCrLf & vbCrLf & "Pause")

        'This was comented out because it did not work

        'The thread will not allow the form to update


        'Call the sub that will actually do the copying

        UpdateFolder(fromPath, toPath, label, progressbar)

    End Sub

    Private Sub RefreshAllPBs(ByVal sender As Object, ByVal e As EventArgs)






    End Sub

End Class

Open in new window

Question by:CanvasYou
  • 5
  • 4

Accepted Solution

kewlchap_85 earned 500 total points
ID: 22759675
I would suggest using backgroundworker tool available in the toolbar... easy to use... and very simple... In case that is an option then do let me know... I will give you more info on that...

Author Comment

ID: 22759754
I will have to research background worker tool -
Basically I need the folders to copy each on a sep thread (4 threads total)
and their progress is updating the progress bars -
the group 1 - group 4 labels are updated showing the files as they copy over.
Does that make sense?
I will see what I can find online about background worker tool.
Is it in VB 2008 Express Edition?
Thank you for your time!

Expert Comment

ID: 22760556
Hi... glad I could help... I know its there in visual studio 2005... didn't use2008 yet...but its really a great tool... I ll c if I can get a good manual for you...


Author Comment

ID: 22761871
I have been reading the MSDN about doWork and backgroundWorker - I am still playing with it -
do you know if it will be able to react w/ the form though? because it is on a sep. thread? Like update the progressbar and labels w/ the file that is currently being copied?
I will keep messing with it - Thanks!
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


Author Comment

ID: 22762007
444: UpdateFolder(fromPath, toPath, label, progressbar)
Returns the error:
Cross-thread operation not valid: Control 'frmPhotoCopy' accessed from a thread other than the thread it was created on.

So the background worker is still trying to access the form' objects - the same way the thread was -
and it is not allowed to do this.

any suggestions?
I know you have to do it with the progress for the backgroundworker - and have it return the progressbar's value
and also return the file name - but I can't work it out.

Thanks in advance!

Author Closing Comment

ID: 31507829
Please see ID:23831567
The backgroundWorker still does not allow me to modify objects -
I am still having issues with it...
Thanks for the help.

Expert Comment

ID: 22765442
yes... during your form load just put this line

 Control.CheckForIllegalCrossThreadCalls = False

this will solve your problem...

Expert Comment

ID: 22765455

Author Comment

ID: 22768245
Okay I will give it a try:
Control.CheckForIllegalCrossThreadCalls = False


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
A short film showing how OnPage and Connectwise integration works.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now