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 7811713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:16:07+00:00 2026-06-02T04:16:07+00:00

I am creating an auto-complete control that updates as the user types text. Every

  • 0

I am creating an auto-complete control that updates as the user types text. Every time the user types a key a new thread fires in order to filter the results. The results come over the network and are a lot therefore it takes about 1 minute to filter. This is something similar of what I have:

    object _lock = new object();
    volatile static bool isThreadRunning=false ;
    private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {

        var text = textBox1.Text.ToUpper();
        ThreadPool.QueueUserWorkItem((o) =>
        {

            lock (_lock) // avoid multiple threads to be running at the same time
            {
                isThreadRunning = true;

                // do work in here

                isThreadRunning=false;
            }

        },text);
    }

So as you can see if I where to quickly type “Hello” then 5 threads will be created and 4 of them will have to wait for the first one to finish. Once that thread finishes the next one will continue to execute and so on.

If there exists 4 threads waiting to execute I will like to only execute the last one. Also the threads enter the lock in random order. How could I determine which is the last one. If a new thread fires and a thread is currently executing maybe I could cancel that thread somehow and that way the order will always be correct. How can I implement that algorithm?


EDIT

This is what I have worked out:

    class DoWorkOnce
    {

        static object _lock = new object();
        static Stack<Action> Work = new Stack<Action>();

        public static void Add(Action task)
        {
            Work.Push(task);
            DoSomeWork();                
        }

        private static void DoSomeWork()
        {                
            Task.Factory.StartNew(() =>
            {
                lock (_lock) // ensure that the following block of code is only executed at once
                {
                    if (Work.Count == 0) // if there is no items in the stack return
                        return;

                    Work.Pop()(); // get the last item in the stack and execute it


                    // remove the bottom of the stack by poping everything exept the top item
                    Action temp=null;
                    if(Work.Count>0)
                        temp = Work.Pop();

                    while (Work.Count > 0)
                        Work.Pop();

                    if (temp != null)
                        Work.Push(temp);
                }
            });
        }
    }

and I can use that class as:

        string[] simulationOfTyping = new string[] { "H", "He", "Hel", "Hell", "Hello", "Hello ", "Hello W", "Hello Wo", "Hello Wor", "Hello Worl", "Hello World" };

        // simulate user typing
        foreach(var text in simulationOfTyping)
        {
            Thread.Sleep(50);
            DoWorkOnce.Add(() =>
            {
                Console.WriteLine(text);
                Thread.Sleep(1000);
            });
        }

        // the last item should execute always.
  • 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-06-02T04:16:09+00:00Added an answer on June 2, 2026 at 4:16 am

    Aside from mellamokb’s comment, it sounds like you don’t really want a thread pool at all – you want a single thread, with a single “next task to execute” – where if there’s already a task to execute, it’s overwritten by the new one. Essentially like a producer/consumer queue, but with only one “slot” which is overwritten.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a user control in ASP.NET (using VB) that uses the autocomplete
What are the steps to creating an auto-suggest text field that grabs for instance
I'm using c# and creating an auto downloader/updater, that checks to see if the
I'm creating an auto complete and I'm having a little problem the results highlighting.
How would I go about creating an auto-complete field in a PHP form? I
I'm creating an autocomplete from a page that returnes values as a text list,
I'm new to ASP.NET MVC and I'm creating an app that will search a
First off, forgive my English, my attempt, I am creating a autocomplete user control,
I am creating an auto-complete box, which uses a list of valid values (objects,
I am currently creating an IDE using Scintilla for the Text component. But autocomplete

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.