Solved

VB.NET form freezes when running big commands

Posted on 2004-08-13
4
659 Views
Last Modified: 2012-05-05
I have a search command that searches thru folders and subdirectories and I cannot move the form till it is done. How can I make it not freeze the window ?
0
Comment
Question by:Vartana
  • 3
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11796986
You have two basic options:

1) Place Application.DoEvents() in your For...Next loop (or in your recursive subroutine) to allow your application to process messages such as moving the form or mouse clicks, etc.

2) Place the search routine in another thread.

Idle_Mind
0
 
LVL 1

Author Comment

by:Vartana
ID: 11797028
2) Place the search routine in another thread.

Example please ?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 11797096
This would make the label go from 1 to 100000 while still allowing the UI to be responsive.  If you called threadedRoutine directly the UI would lock up until the loop completed.

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim t As New System.Threading.Thread(AddressOf Me.threadedRoutine)
        t.Start()
    End Sub

    Private Sub threadedRoutine()
        Dim i As Integer

        For i = 1 To 100000
            Label1.Text = i
        Next
    End Sub
End Class

If you don't want to use another thread then you could change threadedRoutine() to include Application.DoEvents() like this:

    Private Sub threadedRoutine()
        Dim i As Integer

        For i = 1 To 100000
            Label1.Text = i
            Application.DoEvents()
        Next
    End Sub

Regards,

Idle_Mind
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11797118
As a side note...using the Thread method, the Label continues to update as the form is moved.  Using the Application.DoEvents() method, the Label is not updated again until the form has stopped being dragged.

Idle_Mind
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now