0

I am studying VB.NET and threading.

I tried thread method to dynamically update status on GUI part.

However, GUI randomly freeze after 10-20 mins running.

I am not sure what i missing.

On form1 : I have textbox and label to update on GUI

Imports System.IO
Imports System.Threading
Public Class form1

package.add("sleep_time",3)
Private Sub BT_START_Click(sender As Object, e As EventArgs) Handles BT_START.Click 
          Start(package)
end sub


  public sub Start(package as hashtable)
     Dim t As System.Threading.Thread
     t = New Thread(New ThreadStart(Sub() Update.run(Me, Package)))
     t.IsBackground = True
     t.Start()

end sub 
End Class

On run.vb file we update label: Label_status and textbox : text_box_status

Imports System.Threading
Imports System.IO
Public Class Update
  public sub run(myform as form1, pakcage as hashtable)
     for i to 10000 
          myform.Label_status.text = "Try: " + i.tostring()
          update_text_box(myform.text_box_status, "Try: " + i.tostring()) 
          Thread.Sleep(3* 1000)
     end for 
end sub

public sub update_text_box(MF as textbox, SYS_MSG as string)
    System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
    MF.AppendText(SYS_MSG & vbNewLine)
    MF.Select(MF.TextLength, 0)
    MF.ScrollToCaret()
    files.Write(SYS_MSG & vbNewLine)
end sub 

end class

i check Thread updating GUI freezes in random moments Windows Form application freeze randomly when run overnight

I am assuming that I am missing invoke or delegate methods

However, I am not clear sure why this happens. Is there anything that i missed to use thread and update Label and textbox on form?

thanks!

Eric
  • 1
  • 1
  • 3
  • You're running a loop that on every pass (10000 times) updates the GUI and then adds a 3-second delay. Why would you expect that NOT to interfere with your program? – Ken White Apr 08 '22 at 20:52
  • @KenWhite . This is example from my code. I skip some parts to simplify to post. It will be really helpful to me if you provide example. because i just started to learn more about VB.NET. thanks! – Eric Apr 08 '22 at 20:54
  • That's not how it works here. We don't help solve problems with code you just made up. Post your **actual code** in the form of a [mre] that demonstrates the issue if you want help. There are also many existing questions here about multithreading with VB.Net that you can find with a little search effort. – Ken White Apr 08 '22 at 20:57
  • @KenWhite I changed. Like i said there is not that much code on program. And I searched but i wasn't find solutions as beginner. thanks – Eric Apr 08 '22 at 21:51
  • Doing `CheckForIllegalCrossThreadCalls = False` doesn't make it legal. It is only for rare circumstances where you know that the specific calls you are making are safe. What you are doing is not safe. You simply cannot update the UI from a non-UI thread. – Enigmativity Apr 09 '22 at 00:37

1 Answers1

0

Try it like this instead:

private t As System.Threading.Thread

public sub Start(package as hashtable)
    if isnothing(t) then
        t = New Thread(New ThreadStart(Sub() Update.run(Me, Package)))
        t.IsBackground = True
        t.Start()
    end if
end sub

With:

Public Sub run(myform As Form1, package As Hashtable)
    For i As Integer = 1 To 10000
        Dim msg As String = "Try: " + i.ToString()
        myform.Invoke(Sub()
                          myform.Label_status.text = msg
                          Dim MF As TextBox = myform.text_box_status
                          MF.AppendText(SYS_MSG & vbNewLine)
                          MF.Select(MF.TextLength, 0)
                          MF.ScrollToCaret()
                          update_text_box(myform.text_box_status, msg)
                      End Sub)

        files.Write(SYS_MSG & vbNewLine)
        Thread.Sleep(TimeSpan.FromSeconds(3).TotalMilliseconds)
    Next
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • This does nothing to explain to the OP why his form was freezing. There is also no explanation as to why this resolves the issue nor any discussion on the limitations of this approach. It's not a great answer at the moment. – Enigmativity Apr 09 '22 at 00:54
  • It's also not a great question as the "edited" version of the code is also not their actual code. Too many issues here to address. I'm just throwing a slop answer at a slop question; not going to spend more time explaining it. =) – Idle_Mind Apr 09 '22 at 01:17