Click here to Skip to main content
15,894,630 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I am creating a small App with UI on VB.Net (VisualStudio 2010 Ultimate). In this App i am doing mainly math jobs. The main goal is to keep it fast as possible even processing large amounts of data, alright.
I have so far the main Form and a Module, on which i do the calculations, because i think the code on the Module can process things faster and on my Form Class i do the job for the UI.

For example i have declared a Public 'Long' variable on the Module and there is the Sub too which does the calculations, which is triggered by an excecution of a Thread. All runs fine the Long variable holds the right value generated by the Sub via a DO ... Loop Until Boolean.
But when i try to display this value on a Label, it frozed the whole UI til end. Now i resolved the problem with the freezing, but yet the Label.Text itself freezes when it comes to display current values of that variable, only at the end it refreshes. I still dont find a solution for this. The Delegate method seems not to work, Application.DoEvents() is time consuming and it neither works for me, BackgroundWorker, i dont really understand this hack.
Obviously it seems that multiple controls are trying to have access to a certain control, for this causes instability and freeze. Any idea please let me know, thank you
Below you see some snippets of my code on which the problem should lie;

The varibale counter is located on the Module, which is updated by a Thread and which works like a charm, all the other code below is on the Form Class
I need this to give the User ability to follow the progress of certain procedures, in some cases i will update the Label.Text less that in realtime to avoid ages of procedures.
Public counter As Long = 0 ' generated by Do-Loop<...>  Statement

Private Delegate Sub UpdateLabelDelegate(ByVal text As String)
' Goal is to display the value of the Long variable 'counter'

Private Sub lbUpdater(ByVal text As String)
    If lbProgress.InvokeRequired Then
        lbProgress.Invoke(New UpdateLabelDelegate(AddressOf lbUpdater), text)
    Else
        lbProgress.Text = text
    End If
End Sub

Private Sub refresher()
    Do While finished = False
        lbUpdater(counter.ToString)
    Loop
    endThread()
End Sub

Dim t As New System.Threading.Thread(AddressOf refresher)
Public Sub startThread()
    t.Start()
End Sub

Public Sub endThread()
    t.Abort()
End Sub
Posted
Comments
CHill60 1-Mar-13 5:29am    
You could try
lblProgress.Text = text
lblProgress.Refresh()

1 solution

add UIButton and lbCounter on form

VB
    ' Functions to update the UI using the UI thread.
#Region "UI Update Functions"
    Private callingThread As Integer
    Dim sCounter As String = ""

    Private Sub UIButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UIButton.Click
        ' Update the UI from the UI thread.
        callingThread = System.Threading.Thread.CurrentThread.ManagedThreadId()
        For cnt As Long = 1 To 10000
            sCounter = cnt.ToString
            UpdateUI()
            Application.DoEvents()
        Next
    End Sub

    ' The Delegate that is invoked by the control on the form that needs to be updated.
    Delegate Sub UIDelegate()

    Private Sub UpdateUI()
        ' If InvokeRequired is true then the call is being made on a thread other
        ' than the UI thread.
        If lbCounter.InvokeRequired Then
            ' Call UpdateUI by invoking a delegate with the UI control
            callingThread = System.Threading.Thread.CurrentThread.ManagedThreadId()
            Dim newDelegate As New UIDelegate(AddressOf UpdateUI)
            lbCounter.Invoke(newDelegate)
        Else
            lbCounter.Text = sCounter
        End If
    End Sub

#End Region
 
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