Link to home
Start Free TrialLog in
Avatar of CanvasYou
CanvasYou

asked on

Another Cross Threading - update object question!

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
'http://msdn.microsoft.com/en-us/library/aa719970(VS.71).aspx

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

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)
        T.Start()
 
 
 
    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..."
        Try
            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()
                Try
 
                    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
                    label.Refresh()
                    Me.Refresh()
 
 
                Catch ex As IOException
                    If sFile.LastWriteTime > File.GetLastWriteTime(dest & sFile.Name) Then
                        sFile.CopyTo(dest & sFile.Name, True)
                    Else
                        '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
                        label.Refresh()
                        Me.Refresh()
                    End If
                Catch ex As Exception
                    MsgBox("Folder Copy Error #75" & vbCrLf & Err.Description)
                    label.Text = "Error: #75"
                    frmMain.Verbose("Error: #75     " & Err.Description)
                    Me.Refresh()
 
                End Try
 
                pb.PerformStep()
                Me.Refresh()
            Next
            label.Text = "Copy of: " & source & " to: " & dest & vbCrLf & "Complete!"
 
        Catch
            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
        Me.Refresh()
 
 
        '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)
        Me.Refresh()
        ProgressBar1.Refresh()
        ProgressBar2.Refresh()
        ProgressBar3.Refresh()
        ProgressBar4.Refresh()
 
    End Sub
 
 
 
End Class

Open in new window

photo-Update-Tool.JPG
ASKER CERTIFIED SOLUTION
Avatar of kewlchap_85
kewlchap_85
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CanvasYou
CanvasYou

ASKER

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!
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...

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!
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!
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.
yes... during your form load just put this line

 Control.CheckForIllegalCrossThreadCalls = False

this will solve your problem...
Okay I will give it a try:
Control.CheckForIllegalCrossThreadCalls = False

Thanks