Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-14
4
Medium Priority
?
766 Views
Last Modified: 2013-11-27
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.
0
Comment
Question by:olinccu
  • 2
4 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1200 total points
ID: 24850335
You will need to use threading which can be an advance topic for you.

Maybe you can start with the BackgroundWorker component which is a wrapper to ease threading.

Have a look at http://www.emoreau.com/Entries/Articles/2006/12/The-BackgroundWorker-component.aspx
0
 
LVL 1

Assisted Solution

by:FragReaper
FragReaper earned 400 total points
ID: 24881515
Add a new module and run the function in a new thread. But you can't pass variables to a function in a new thread, so you put it in a new class.
Imports System.IO
 
Public Class NewThread
Private InformationReturned as string 
Private PathToJpeg as string 'This is how we will pass information to and from the function
 
Public Event Done(ByVal result as string) ' this is the event that will run on the thread being completed
 
Public WriteOnly Property PassedPathToJpeg() as string
Set(ByVal PassedPathToJpeg As String)
PathToJpeg = PassedPathToJpeg 
End Set
End Property
 
Public ReadOnly Property  PassedInformationReturned() as string
Set(ByVal InformationReturned as String)
PassedInformationReturned = InformationReturned
End set
End Property
 
(Your function goes here)
 
Private Sub NewThread_Done() Handles Me.Done
'to be executed on function completion in new thread
End sub
End class
 '''''''''
 
'And then in your main thead code:
 
Sub CallNewThread()
        Dim oThread As System.Threading.Thread
        Dim oProcess As New MultiThread
oThread = New Thread(AddressOf oProcess.NewThreadWorkings)
                oProcess.pathtojpeg = ' your path
                oThread.Priority = ThreadPriority.' Priority
                oThread.Start()
End Sub

Open in new window

0
 
LVL 3

Expert Comment

by:Kraeven
ID: 24907217
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

0
 
LVL 3

Assisted Solution

by:Kraeven
Kraeven earned 400 total points
ID: 24907238
Sorry messed up previous post...
Ignore it please

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)
        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
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question