Valhalla Legends Forums Archive | .NET Platform | [SOLVED] VB.Net program "freeze"

AuthorMessageTime
Zer0
Ok, my example i made a hash manager and while the hashes are downloading the program like "freezes" i remember at one time seeing there was a way to fix this but i cant remember what it was heres the code if it helps any

[code]
    Private Sub Hashstar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hashstar.Click
        hashStatus.Text = "Downloading..."
        Dim wc As New System.Net.WebClient()
        Directory.CreateDirectory(App_Path() + "/star")
        hashOne.Text = "Starcraft.exe Exists?"
        hashBox1.Text = File.Exists(App_Path() + "/star/starcraft.exe").ToString()
        If hashBox1.Text = False Or hashOverite.Text = "Yes" Then
            wc.DownloadFile("http://www.***zer0.com/hash/star/starcraft.exe", _
                     App_Path() + "/star/starcraft.exe")
            hashBox1.Text = "Downloaded"
        End If
        hashTwo.Text = "Battle.snp Exists?"
        hashBox2.Text = File.Exists(App_Path() + "/star/battle.snp").ToString()
        If hashBox2.Text = False Or hashOverite.Text = "Yes" Then
            wc.DownloadFile("http://www.***zer0.com/hash/star/battle.snp", _
                     App_Path() + "/star/battle.snp")
            hashBox2.Text = "Downloaded"
        End If
        hashThree.Text = "Storm.dll Exists?"
        hashBox3.Text = File.Exists(App_Path() + "/star/storm.dll").ToString()
        If hashBox3.Text = False Or hashOverite.Text = "Yes" Then
            wc.DownloadFile("http://www.***zer0.com/hash/star/storm.dll", _
                     App_Path() + "/star/storm.dll")
            hashBox3.Text = "Downloaded"
        End If
        verCheck = File.Exists(App_Path() + "/star/ver.txt").ToString()
        If verCheck = False Or hashOverite.Text = "Yes" Then
            wc.DownloadFile("http://www.***zer0.com/hash/star/ver.txt", _
                     App_Path() + "/star/ver.txt")
        End If
        If hashOverite.Text = "Yes" Then
            hashBox3.Text = "Overwritten"
            hashBox2.Text = "Overwritten"
            hashBox1.Text = "Overwritten"
        End If
        hashRefresh()
        hashStatus.Text = "Done SC..."
    End Sub
[/code]
October 10, 2006, 6:11 AM
Myndfyr
Do you know the line at which it's freezing?  Have you debugged at all?

One possible issue might be that your code may be executing on a thread other than the thread on which the GUI was created.  GUI components must only be updated on the thread that created them (the main thread).  This is done by creating a callback delegate and then marshaling to the delegate via control.Invoke(delegateInstance). 
October 10, 2006, 8:09 AM
Zer0
i dont think i worded that very well, but it is 100% working i just want you to be able to do other things and not have the program stop while the files download
October 10, 2006, 2:55 PM
St0rm.iD
you basically want to spin off each of those calls into a separate thread and join them at the end when you display the status notification. no idea how to do that in .net; it's easy in java.
October 10, 2006, 3:04 PM
Zer0
ok i finally figured out how to do it, now i have another problem with the same thing, i am using a richtextbox to log the hash managers action so you can know when it is done and so on but it dont seem to likem e doing that i keep getting

[code]"Cross-thread operation not valid: Control 'hashLog' accessed from a thread other than the thread it was created on."[/code]

heres the code im using (very basic threading)

[code]
    Private Sub Hashstar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hashstar.Click
        hashStatus.Text = "Downloading..."
        Dim wc As New System.Net.WebClient()
        Directory.CreateDirectory(App_Path() + "/star")

        ThreadTest = New System.Threading.Thread(AddressOf sc_start)
        ThreadTest.Start()
       
        hashRefresh()
        hashStatus.Text = "Done SC..."
    End Sub

    Public Sub sc_start()
        dl_file("star", "starcraft.exe")
        dl_file("star", "battle.snp")
        dl_file("star", "storm.dll")
        dl_file("star", "ver.txt")
    End Sub

    Public Function dl_file(ByVal hash As String, ByVal filen As String)
        If hash <> "" And filen <> "" Then
            Dim wc As New System.Net.WebClient()
            Dim filecheck As String
            filecheck = File.Exists(App_Path() + "/" + hash + "/" + filen).ToString()
            If filecheck = False Then
                wc.DownloadFile("http://www.allieszer0.com/hash/" + hash + "/" + filen, _
                         App_Path() + "/" + hash + "/" + filen)
                log_hashes(hash + "/" + filen + " : Downloaded" + vbCr)
            ElseIf filecheck = True Then
                log_hashes(hash + "/" + filen + " : Already Exists" + vbCr)
            End If
        End If
        Return True
    End Function

    Public Function log_hashes(ByVal log As String)
        hashLog.Text = hashLog.Text + log
        Return True
    End Function
[/code]

that is every function involved so far, i havent tried the threading on ne thing but SC yet
October 10, 2006, 4:56 PM
Myndfyr
[quote author=Zer0 link=topic=15849.msg159630#msg159630 date=1160499371]
i keep getting

[code]"Cross-thread operation not valid: Control 'hashLog' accessed from a thread other than the thread it was created on."[/code]
[/quote]
Oh, really?  Hrm...

[quote author=MyndFyre[vL] link=topic=15849.msg159624#msg159624 date=1160467743]
One possible issue might be that your code may be executing on a thread other than the thread on which the GUI was created.  GUI components must only be updated on the thread that created them (the main thread).  This is done by creating a callback delegate and then marshaling to the delegate via control.Invoke(delegateInstance). 
[/quote]

Hrm...

My guess is that you can change your log_hashes(String) function to do what you need to do:
[code]
' Add this delegate:
Private Delegate Sub HashLogCallback(ByVal log As String)

Private Sub log_hashes(ByVal log As String)
  Dim cb As New HashLogCallback(AddressOf log_implementation)
  If hashLog.InvokeRequred Then
    hashLog.Invoke(cb, log)
  Else
    cb(log)
  End If
End Sub

' Add this subroutine
Private Sub log_implementation(ByVal log As String)
  hashLog.Text = hashLog.Text & log
End Sub
[/code]
October 10, 2006, 6:33 PM
Zer0
thank ya myndfyre worked like a charm :)
October 10, 2006, 7:09 PM
Myndfyr
[quote author=Zer0 link=topic=15849.msg159632#msg159632 date=1160507397]
thank ya myndfyre worked like a charm :)
[/quote]
That's pretty much the most common thing I hear about GUIs.
October 10, 2006, 7:11 PM

Search