Solved

VB.NET form freezes when running big commands

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

Industry Leaders: 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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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