• Welcome to Valhalla Legends Archive.
 

[SOLVED] VB.Net program "freeze"

Started by Zer0, October 10, 2006, 01:11 AM

Previous topic - Next topic

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


    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

MyndFyre

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). 
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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

Banana fanna fo fanna

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.

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

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

heres the code im using (very basic threading)


    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


that is every function involved so far, i havent tried the threading on ne thing but SC yet

MyndFyre

Quote from: Zer0 on October 10, 2006, 11:56 AM
i keep getting

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

Quote from: MyndFyre[vL] on October 10, 2006, 03:09 AM
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). 

Hrm...

My guess is that you can change your log_hashes(String) function to do what you need to do:

' 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
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Zer0

thank ya myndfyre worked like a charm :)

MyndFyre

Quote from: Zer0 on October 10, 2006, 02:09 PM
thank ya myndfyre worked like a charm :)
That's pretty much the most common thing I hear about GUIs.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.