Link to home
Start Free TrialLog in
Avatar of olinccu
olinccu

asked on

Visual Basic .NET - stop program from freezing during function execution

I have a program that is going to open JPEG files in a directory and analyze them (it analyzes the color content) and updates a textbox with some information.  As the program is looping through the JPEG files, I've used the System.Windows.Forms.Application.DoEvents() method so that the output is sent to the textbox as the program is looping through the files.  My question is, how do I stop the entire program from freezing.  When the loop is running, I cannot move the form window or click any other buttons on the form.  Keep in mind that i'm a programming beginner, so I need some hand-holding to help me out.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The easy way is :

In the sub/function/event that would call the function (remove the function call) that starts to loop through the jpg's and call an other sub like this one :


Private Sub StartProcessingThread()
Dim t as new threading.thread(addressof StartThread)
t.start()
end sub
 
Private sub StartThread()
dim InfoToTextBox as string
'here goes your code processing the jpg's
...
...
'fill in the var InfoToTextbox with whatever info you want to display in the textbox
'for instances
infoToTextbox = "JPG processed..."
 
'call sub to update progress
updateprogress(infoToTextbox)
End sub
 
'Delegate needed to access controls across threads
Private Delegate Sub ChangeTextbox1Callback(ByVal log As String)
 
  Public Sub updateprogess(ByVal log As String) handles me.done
        If Textbox1.InvokeRequired Then
  ' instantiate the callback instance out of this very method
     Dim callback As New ChangeTextbox1Callback(AddressOf updateprogess)
 
     ' invoke it, when it comes to it again InvokeRequired will be false
            Me.Invoke(callback, New Object() {log})
          Else
            'display text in textbox
             Textbox1.text = textbox1.text + vbcrlf + log
        End If
    End Sub

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial