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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:31:51+00:00 2026-06-13T09:31:51+00:00

I am trying to asynchronously resolve an IP address to host name using C#

  • 0

I am trying to asynchronously resolve an IP address to host name using C# and then add the result to a ListBox. The asynchronous operation works alright but i can not add the resolved name to a listbox because another thread owns it. How can i bypass this issue and add the resolved name to a listbox.

here is what i’ve done so far

public static ManualResetEvent GetHostEntryFinished = new ManualResetEvent(false);            

            private void AsyncDNSResolver(string IPString)
        {
        GetHostEntryFinished.Reset();

        IPHostEntry host = new IPHostEntry();
        host.AddressList = new IPAddress[] { IPAddress.Parse(IPString) };
        Dns.BeginGetHostEntry(host.AddressList[0], new AsyncCallback(GetHostEntryCallback), host);

        GetHostEntryFinished.WaitOne();

    }

    public static void GetHostEntryCallback(IAsyncResult ar)
    {
        IPHostEntry host = (IPHostEntry)ar.AsyncState;
        while (!ar.IsCompleted) ;
        host = Dns.EndGetHostEntry(ar);
       lsvHosts.items.add(host.HostName);
     GetHostEntryFinished.Set();
    } 
  • 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-13T09:31:52+00:00Added an answer on June 13, 2026 at 9:31 am

    If you wanted to execute an expensive activity on a separate thread and continue working while waiting for the result you can use this code snippet.

    static void Main(string[] args)
    {
        Console.WriteLine("About to call DNS");
        ResolveDnsAndPerformWorkWhileWaiting("74.125.237.112");
        Console.Read();
    }
    
    public static void ResolveDnsAndPerformWorkWhileWaiting(string ipaddress)
    {
        var task = new Task<string>(() =>
            {
                var host = new IPHostEntry { AddressList = new[] { IPAddress.Parse(ipaddress) } };
                var result = Dns.GetHostEntry(host.AddressList[0]);
                return result.HostName;
            });
    
        // execute the work on another thread
        task.Start();
    
        // do other stuff
    
        // use the results (if the task has not finished the .Result property will hold until completed)
        var resolvedAddress = task.Result;
        Console.WriteLine("Resolved address:" + resolvedAddress);
    }
    

    If you are using a Windows Forms application then the easiest way would be to use a BackgroundWorker, here is a code sample

    static void Main(string[] args)
    {
        Console.WriteLine("About to call DNS");
    
        var worker = new BackgroundWorker();
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.RunWorkerAsync("74.125.237.112");
    
    
        Console.Read();
    }
    
    static void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        var host = new IPHostEntry { AddressList = new[] { IPAddress.Parse((string)e.Argument) } };
        var result = Dns.GetHostEntry(host.AddressList[0]);
    
        e.Result = result.HostName;
    }
    
    static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // You could set your UI control here like label1.text = (string)e.Result
        Console.WriteLine((string)e.Result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to asynchronously resolve a ftp host using Boost.Asio. Here's what I've tried
I am trying to make asynchronous calls using xmlhttprequest object so it completely works
I am trying to asynchronously query a provider by using a CursorLoader with a
I am trying to send E-mails asynchronously and it works fine as long as
I'm trying to model a asynchronous job processing framework using MailboxProcessor. My requirements are
I am trying to implement an Asynchronous file download from a web server using
I'm trying to add items to a JList asynchronously but I am regularly getting
I am trying to process some tasks asynchronously using Rx, e.g. var list =
I'm trying to find a way to asynchronously load an image and then instead
I'm trying to load an image asynchronously using jquery javascript framework. The loaded image

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.