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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:50:53+00:00 2026-06-18T08:50:53+00:00

I have a piece of code that searches several third party APIs. I have

  • 0

I have a piece of code that searches several third party APIs. I have the searches split into 2 groups based on the search criteria. I start both searches because each search is quite timely, but if the first group of searches results in an match I don’t want to wait around for the second search group to finish. So basically what I have is:

Dictionary<string, string> result = null;
NameSearchDelegate nameDel = new NameSearchDelegate(SearchByName);
IAsyncResult nameTag = nameDel.BeginInvoke(name, null, null);
if(!string.IsNullOrWhiteSpace(telNum))
{
    result = SearchByTelNum(telNum);//Will return null if a match is not found
}
if(null == result)
{
    result = nameDel.EndInvoke(nameTag);
}
//End the delegate to prevent memory leak
//else
//{
//    nameDel.EndInvoke(nameTag)
//}
return result;

So I want to start the SearchByName before I call SearchByTelNum in case it does not find a match, however if it does find a match I don’t want to have to wait for SearchByName to finish before returning the match. Is there any way to simply end or cancel that delegate if I no longer need its result?

  • 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-18T08:50:54+00:00Added an answer on June 18, 2026 at 8:50 am

    I was able to solve my problem using System.ComponentModel.BackgroundWorker. I wasn’t necessarily using it in the way it is intended to be used but it was able to do what I needed. So basically what my new code looks like is:

    Dictionary<string, string> telResult = null,
                               nameResult = null;
    
    BackgroundWorker bw = new BackgroundWorker();
    bw.WorkerSupportsCancellation = true;
    bw.DoWork += (obj, e) => nameResult = SearchByName(name, bw);
    bw.RunWorkerAsync();
    
    if(!string.IsNullOrWhiteSpace(telNum))
        telResult = SearchByTelNum(telNum);//Will return null if a match is not found
    
    if(telResult != null)
    {
        bw.CancelAsync;
        return telResult;
    }
    
    bool hasTimedOut = false;
    int i = timeOutCount;
    while (bw.IsBusy && !hasTimedOut)
    {
        System.Threading.Thread.Sleep(500);
        if (0 == --i) hasTimedOut = true;
    }
    
    return nameResult;
    

    And to make sure there are no bugs, I had to make sure that SearchByName periodically checks if bw.CancellationPending equals true, and ends the method in that case. CancelAsync does not end the worker thread, it simply alerts the worker thread that caller thread has canceled it.

    Also I could have simply used

    while(bw.IsBusy) System.Threading.Thread.Sleep(500)
    

    to wait for the method to complete, but if something bad happens in SearchByName you could end up waiting forever in an infinite loop. This way I can set an amount of time before the method is considered to have timed out and the caller thread just goes on with life. In this case, since I check bw.IsBusy every .5 seconds, the timeout length is equal to timeOutCount / 2 seconds.

    Ok I think I have thoroughly answered my own question.

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

Sidebar

Related Questions

I have a piece of code that takes several rows from a database and
I have this piece of code that searches for a folder given a starting
I have this piece of code that converts an ArrayList of StringBuilders into an
I have a piece of code that was written by someone else before the
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have a piece of code that load a very big image in memory.
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have a piece of code that calls a WCF service that is hosted
If have a piece of code that gets some data from a sql database

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.