?
Solved

VB.NET form freezes when running big commands

Posted on 2004-08-13
4
Medium Priority
?
728 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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