none
Avoid program from becomming unresponsive RRS feed

  • Question

  • I have a program that searches for some file in entire drive, but my program becomes unresponsive while searching the file, is there anything by using a thread so my program stays responsive ..

    Any help will be greatly appreciated

    Thanks

    Sunday, March 24, 2013 8:46 PM

Answers

  • I have a program that searches for some file in entire drive, but my program becomes unresponsive while searching the file, is there anything by using a thread so my program stays responsive ..

    Any help will be greatly appreciated

    Thanks

    Make your program multi-threaded, or use a background worker.

    example:

    Option Strict On
    Imports System.Threading
    Public Class Form1
        Delegate Sub ChangeControlText(ByVal Control As Control, ByVal Text As String)
        Sub UpdateText(ByVal Control As Control, ByVal Text As String)
            If Control.InvokeRequired Then
                Control.Invoke(New ChangeControlText(AddressOf UpdateText), Control, Text)
            Else
                Control.Text = Text
            End If
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Th As New Thread(AddressOf Threadwork)
            Th.Start()
        End Sub
        Sub Threadwork()
            For I = 0 To 10000
                UpdateText(Label1, I.ToString)
            Next
        End Sub
    End Class


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Sunday, March 24, 2013 8:56 PM
    Moderator
  • Hi, you may use Application.Doevents() method in the loop that does searching.

    Sunday, March 24, 2013 10:20 PM

All replies

  • I have a program that searches for some file in entire drive, but my program becomes unresponsive while searching the file, is there anything by using a thread so my program stays responsive ..

    Any help will be greatly appreciated

    Thanks

    Make your program multi-threaded, or use a background worker.

    example:

    Option Strict On
    Imports System.Threading
    Public Class Form1
        Delegate Sub ChangeControlText(ByVal Control As Control, ByVal Text As String)
        Sub UpdateText(ByVal Control As Control, ByVal Text As String)
            If Control.InvokeRequired Then
                Control.Invoke(New ChangeControlText(AddressOf UpdateText), Control, Text)
            Else
                Control.Text = Text
            End If
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Th As New Thread(AddressOf Threadwork)
            Th.Start()
        End Sub
        Sub Threadwork()
            For I = 0 To 10000
                UpdateText(Label1, I.ToString)
            Next
        End Sub
    End Class


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Sunday, March 24, 2013 8:56 PM
    Moderator
  • Hi, you may use Application.Doevents() method in the loop that does searching.

    Sunday, March 24, 2013 10:20 PM