Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6639001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:30:11+00:00 2026-05-25T23:30:11+00:00

I’ve searched but I can’t find the solution I’m looking for. I specifically want

  • 0

I’ve searched but I can’t find the solution I’m looking for.

I specifically want to use Threading.Tasks.Task for this project, just to understand it better.

I have a lengthy XML file that I want to search based on text that a user types in. Because it’s lengthy, I want to wait for the user to stop typing for 250ms or so before I actually start searching in the background. I tried to kick off my Task, having it sleep for 250ms, then check if my CancelTokenSource had canceled because another character was typed. I’m not seeing a cancellation, though, so I end up seeing my results flash several times as the queued up search tasks complete, one after the other, after I finish typing.

My code has turned into a terrible mess and I need to start over, but was hoping someone could point me in the right direction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:30:12+00:00Added an answer on May 25, 2026 at 11:30 pm

    Start with a thread-safe property that determines when the search should begin. Initialise it to Date.MaxValue to prevent it running before it’s asked to.

    Private Property SearchTriggerTime As Date
        Get
            SyncLock SearchTriggerTimeLock
                Return _SearchTriggerTime
            End SyncLock
        End Get
        Set(value As Date)
            SyncLock SearchTriggerTimeLock
                _SearchTriggerTime = value
            End SyncLock
        End Set
    End Property
    Private _SearchTriggerTime As Date = Date.MaxValue
    Private ReadOnly SearchTriggerTimeLock As New Object
    

    When the search text box text changes, set the timer to when you want to start searching. As the user types quickly, the search timer will be reset before it triggers. In the code below, if the user clears the text box, the timer is set to never trigger, ie. do not search.

    Private Const SearchDelay As Integer = 250
    
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text <> "" Then
            SearchTriggerTime = Date.Now.AddMilliseconds(SearchDelay)
        Else
            SearchTriggerTime = Date.MaxValue
        End If
    End Sub
    

    Start a thread to perform the searching when the form loads.

    Public Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
        With New Thread(AddressOf SearchThread)
            .Start()
        End With
    End Sub
    

    This thread passes through three states. The first state is waiting for the trigger time to start the search. It checks the trigger time every 50ms. The second state is performing the search. During the search, it checks if the form closes or if the user types more, and abandons the search in those cases. In the third state, if the search completes normally, the form’s original thread is asked to display the results. If you need to change a control, always do so on the form’s thread by using Form.Invoke(Delegate).

    Private Sub SearchThread()
        Do Until IsDisposed
            ' Wait for the user to stop typing.
            Do Until IsDisposed OrElse SearchTriggerTime <= Date.Now
                Thread.Sleep(50)
            Loop
            ' Search until the form is disposed, the user types more, or the search is complete.
            ' TODO: Initialise the search variables.
            Dim SearchComplete As Boolean = False
            Do Until IsDisposed OrElse SearchTriggerTime > Date.Now OrElse SearchComplete
                ' TODO: Insert search code here.
                If condition Then SearchComplete = True
            Loop
            ' Reset the search timer.
            SearchTriggerTime = Date.MaxValue
            ' Only display results if the search was completed.
            If SearchComplete Then Invoke(New Action(AddressOf DisplaySearchResults))
        Loop
    End Sub
    

    Lastly, create a method to display your search results. This will be run in the form’s thread to prevent invalid cross-thread operations.

    Private Sub DisplaySearchResults()
        ' TODO: Display search results.
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.