Solved

VB.NET form freezes when running big commands

Posted on 2004-08-13
4
686 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

860 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