Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I seem to be having problems when trying to run the shared sub from another class in my form1 application. The code works flawless when its called directly but when i try to multi thread the controls on form1 will not update.


The shared function and sub

VB
Public Shared Function FileCount(PathName As String) As Long
        Try
            Dim FSO As New FileSystemObject
            Dim fld As Folder
            If FSO.FolderExists(PathName) Then
                fld = FSO.GetFolder(PathName)
                FileCount = fld.Files.Count
            End If
            Return FileCount
        Catch ex As DirectoryNotFoundException
        End Try
    End Function 'Calculate File Count

    Public Shared Sub StartNewScan(ByVal FilePath As String)
        Dim Allfile As String = "*.*"
        Dim FFcount As Long
        Dim MyFolderSize As Long
        Dim FullPath As String
        Using fse As New FindFiles.FileSystemEnumerator(FilePath, Allfile, True)
            Dim ien As IEnumerator(Of FileInfo) = fse.Matches().GetEnumerator()
            ien.Dispose()
            For Each fi As FileInfo In fse.Matches()
                FFcount += CStr(FileCount(fi.DirectoryName)).Length
                Form1.Label3.Text = CStr( "File Count:" & FFcount)
                MyFolderSize += fi.Length
                FullPath = fi.FullName
                Form1.Label4.Text = "File Path:" & FullPath
            Next
        End Using
    End Sub



I am trying to call the shared sub in form1 like so

VB
Dim StartSharedSub as new threading.thread(addressof Startnewscan)
startsharedsub.start()


I am aware of delegates and that it may be necessary in my case to use that to update my UI controls but I am confused how to implement it in my case.

thank you in advance
Posted
Updated 21-Jun-14 6:49am
v2

Don't past the same question in multiple forums. It supplicates work and does not allow for a collaborative answer.
 
Share this answer
 
Comments
Draco2013 21-Jun-14 15:47pm    
ok my apologies I was unsure how visible my question would be on that form so i posted as a quick question also.
Draco2013 21-Jun-14 15:48pm    
any ideas what i can try to solve this problem?..
You need to learn how to work with threads and UI.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900