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

  • Home
  • SEARCH
  • 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 100343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:33:41+00:00 2026-05-11T00:33:41+00:00

I created a custom autocomplete control, when the user press a key it queries

  • 0

I created a custom autocomplete control, when the user press a key it queries the database server (using Remoting) on another thread. When the user types very fast, the program must cancel the previously executing request/thread.

I previously implemented it as AsyncCallback first, but i find it cumbersome, too many house rules to follow (e.g. AsyncResult, AsyncState, EndInvoke) plus you have to detect the thread of the BeginInvoke’d object, so you can terminate the previously executing thread. Besides if I continued the AsyncCallback, there’s no method on those AsyncCallbacks that can properly terminate previously executing thread.

EndInvoke cannot terminate the thread, it would still complete the operation of the to be terminated thread. I would still end up using Abort() on thread.

So i decided to just implement it with pure Thread approach, sans the AsyncCallback. Is this thread.abort() normal and safe to you?

public delegate DataSet LookupValuesDelegate(LookupTextEventArgs e);  internal delegate void PassDataSet(DataSet ds);  public class AutoCompleteBox : UserControl {    Thread _yarn = null;     [System.ComponentModel.Category('Data')]    public LookupValuesDelegate LookupValuesDelegate { set; get; }     void DataSetCallback(DataSet ds)    {       if (this.InvokeRequired)          this.Invoke(new PassDataSet(DataSetCallback), ds);       else       {          // implements the appending of text on textbox here       }    }     private void txt_TextChanged(object sender, EventArgs e)    {       if (_yarn != null) _yarn.Abort();        _yarn = new Thread(          new Mate          {             LookupValuesDelegate = this.LookupValuesDelegate,             LookupTextEventArgs =             new LookupTextEventArgs             {                RowOffset = offset,                Filter = txt.Text             },             PassDataSet = this.DataSetCallback          }.DoWork);        _yarn.Start();    } }   internal class Mate {    internal LookupTextEventArgs LookupTextEventArgs = null;     internal LookupValuesDelegate LookupValuesDelegate = null;     internal PassDataSet PassDataSet = null;      object o = new object();    internal void DoWork()    {       lock (o)       {          // the actual code that queries the database          var ds = LookupValuesDelegate(LookupTextEventArgs);          PassDataSet(ds);       }    } } 

NOTES

The reason for cancelling the previous thread when the user type keys in succession, is not only to prevent the appending of text from happening, but also to cancel the previous network roundtrip, so the program won’t be consuming too much memory resulting from successive network operation.

I’m worried if I avoid thread.Abort() altogether, the program could consume too much memory.

here’s the code without the thread.Abort(), using a counter:

internal delegate void PassDataSet(DataSet ds, int keyIndex);  public class AutoCompleteBox : UserControl {    [System.ComponentModel.Category('Data')]    public LookupValuesDelegate LookupValuesDelegate { set; get; }     static int _currentKeyIndex = 0;     void DataSetCallback(DataSet ds, int keyIndex)    {       if (this.InvokeRequired)          this.Invoke(new PassDataSet(DataSetCallback), ds, keyIndex);       else       {          // ignore the returned DataSet          if (keyIndex < _currentKeyIndex) return;            // implements the appending of text on textbox here...       }    }     private void txt_TextChanged(object sender, EventArgs e)    {       Interlocked.Increment(ref _currentKeyIndex);        var yarn = new Thread(          new Mate          {             KeyIndex = _currentKeyIndex,             LookupValuesDelegate = this.LookupValuesDelegate,             LookupTextEventArgs =             new LookupTextEventArgs             {                RowOffset = offset,                Filter = txt.Text             },             PassDataSet = this.DataSetCallback          }.DoWork);        yarn.Start();    } }   internal class Mate {    internal int KeyIndex;    internal LookupTextEventArgs LookupTextEventArgs = null;    internal LookupValuesDelegate LookupValuesDelegate = null;    internal PassDataSet PassDataSet = null;     object o = new object();    internal void DoWork()    {       lock (o)       {          // the actual code that queries the database          var ds = LookupValuesDelegate(LookupTextEventArgs);          PassDataSet(ds, KeyIndex);       }    } } 
  • 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. 2026-05-11T00:33:41+00:00Added an answer on May 11, 2026 at 12:33 am

    No, it is not safe. Thread.Abort() is sketchy enough at the best of times, but in this case your control has no (heh) control over what’s being done in the delegate callback. You don’t know what state the rest of the app will be left in, and may well find yourself in a world of hurt when the time comes to call the delegate again.

    Set up a timer. Wait a bit after the text change before calling the delegate. Then wait for it to return before calling it again. If it’s that slow, or the user is typing that fast, then they probably don’t expect autocomplete anyway.

    Regarding your updated (Abort()-free) code:

    You’re now launching a new thread for (potentially) every keypress. This is not only going to kill performance, it’s unnecessary – if the user isn’t pausing, they probably aren’t looking for the control to complete what they’re typing.

    I touched on this earlier, but P Daddy said it better:

    You’d be better off just implementing a one-shot timer, with maybe a half-second timeout, and resetting it on each keystroke.

    Think about it: a fast typist might create a score of threads before the first autocomplete callback has had a chance to finish, even with a fast connection to a fast database. But if you delay making the request until a short period of time after the last keystroke has elapsed, then you have a better chance of hitting that sweet spot where the user has typed all they want to (or all they know!) and is just starting to wait for autocomplete to kick in. Play with the delay – a half-second might be appropriate for impatient touch-typists, but if your users are a bit more relaxed… or your database is a bit more slow… then you may get better results with a 2-3 second delay, or even longer. The most important part of this technique though, is that you reset the timer on every keystroke.

    And unless you expect database requests to actually hang, don’t bother trying to allow multiple concurrent requests. If a request is currently in-progress, wait for it to complete before making another one.

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

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Your theoretical code is almost near to my situation with… May 11, 2026 at 11:51 am
  • added an answer Create a triangle control, where you can move a single… May 11, 2026 at 11:51 am
  • added an answer If you mean like the auto-complete options on TextBox, I'm… May 11, 2026 at 11:51 am

Related Questions

I created a custom autocomplete control, when the user press a key it queries
In my VB.net project I created a custom cursor (Window.cur). How can I assign
I created a vbscript custom action which checks for some registry keys and alters
I have created a custom dialog for Visual Studio Setup Project using the steps
I've created a custom exception for a very specific problem that can go wrong.
I've created a custom list, and made some changes to the way the CQWP
I've created a custom ListBox like in here . Thing is it doesn't raise
I've created a custom object, I have it appearing automatically on the Account details
I've created a custom thread pool utility, but there seems to be a problem
So I've created a custom RenderingTemplate and deployed it to CONTROLTEMPLATES\MyControlTemplates\ It basically dictates

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.