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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:56:37+00:00 2026-05-25T05:56:37+00:00

I’m implementing a visual version of Tracert (as a learning exercise) in WPF where

  • 0

I’m implementing a visual version of Tracert (as a learning exercise) in WPF where results go to a listbox. The issues are (1) the listbox bound to tracertDataView is not updating, but (2) my entire application hangs.

I’m sure #2 is a threading issue but I’m not sure how to correct it (in the right way). In addition I’m not sure my technique of updating / binding the results of “DoTrace” are correct.

Here is my datasource in App.xaml

<Window.Resources>
<CollectionViewSource 
          Source="{Binding Source={x:Static Application.Current}, Path=TracertResultNodes}"   
          x:Key="tracertDataView" />

</Window.Resources>

App.xaml.cs

public partial class App : Application
{
    private ObservableCollection<TracertNode> tracertResultNodes = new ObservableCollection<TracertNode>();

    public void AppStartup(object sender, StartupEventArgs e)
    {
          // NOTE: Load sample data does work correctly.. and displays on the screen.  
         //      subsequent updates do not display
        LoadSampleData();
    }

    private void LoadSampleData()
    {

         TracertResultNodes = new ObservableCollection<TracertNode>();

        TracertNode t = new TracertNode();
        t.Address = new System.Net.IPAddress(0x2414188f);
        t.RoundTripTime = 30;
        t.Status = System.Net.NetworkInformation.IPStatus.BadRoute;

            TracertResultNodes.Add(t);
    }

    public ObservableCollection<TracertNode> TracertResultNodes
    {
        get { return this.tracertResultNodes; }
        set { this.tracertResultNodes = value; }
    }
}

Here is the MainWindow code

  public partial class MainWindow : Window
{
    CollectionViewSource tracertDataView;
    TraceWrapper _tracertWrapper = null;

    public MainWindow()
    {
        InitializeComponent();
         _tracertWrapper = new TraceWrapper();

        tracertDataView = (CollectionViewSource)(this.Resources["tracertDataView"]);
    }

    private void DoTrace_Click(object sender, RoutedEventArgs e)
    {
       ((App)Application.Current).TracertResultNodes = _tracertWrapper.Results;

       _tracertWrapper.DoTrace("8.8.8.8", 30, 50);
    }
}

FYI Internal implementation Detail of instance object “traceWrapper.DoTrace”

    /// <summary>
    /// Trace a host.  Note that this object internally calls the Async implementation of .NET's PING. 
    // It works perfectly fine in a CMD host, but not in WPF
    /// </summary>
     public ObservableCollection<TracertNode> DoTrace(string HostOrIP, int maxHops, int TimeOut)
    {
        tracert = new Tracert();

        // The following is triggered for every host that is found, or upon timeout
         //  (up to 30 times by default)
        AutoResetEvent wait = new AutoResetEvent(false);
       tracert.waiter = wait;

        tracert.HostNameOrAddress = HostOrIP;

        tracert.Trace();

        this.Results = tracert.NodeList;

        while (tracert.IsDone == false)
        {
            wait.WaitOne();
            IsDone = tracert.IsDone;
        }
        return tracert.NodeList;
    }
  • 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-25T05:56:38+00:00Added an answer on May 25, 2026 at 5:56 am

    I don’t understand how u used AutoResetEvent, i guess it is not supposed to be used in this way 🙂

    But since Trace run already in another thread, are you sure there is not an event “OnTracertComplete” or something like that in your Tracert class?

    If there is not, why you just don’t put a DispatchTimer into your application?
    That timer would periodically poll until tracert.IsDone becomes true.
    If you block the execution of the application thread until an operation completes, you block the execution of the window event loop so window will never be updated.

    Another important thing: you cannot update ObservableCollections from another thread.
    Be careful and be sure that everything that is updated in the WPF window is executed from the same thread of the window. Don’t know what your Trace class do exactly, but your problem here seems to be of course the wait loop, that don’t makes sense in a GUI application.

    Use notification events or a timer to poll the result. A timer with 1 second resolution seems good to me for this particular implementation and the performance inpact is absolutely minimal.

    This is a possible implementation if you are able to modify the Tracert class.

        public delegate void TracertCallbacHandler(Tracert sender, TracertNode newNode);
    
        public class Tracert
        {
            public event TracertCallbacHandler NewNodeFound;
            public event EventHandler TracertCompleted;
    
            public void Trace()
            {
                ....
            }
    
            // This function gets called in tracert thread\async method.
            private void FunctionCalledInThreadWhenPingCompletes(TracertNode newNode)
            {
                var handler = this.NewNodeFound;
                if (handler != null)
                    handler(this, newNode);
            }
    
            // This function gets called in tracert thread\async methods when everything ends.
            private void FunctionCalledWhenEverythingDone()
            {
                var handler = this.TracertCompleted;
                if (handler != null)
                    handler(this, EventArgs.Empty);
            }
    
        }
    

    And here is the code to run the tracert,
    This is TracertWrapper.

        // Keep the observable collection as a field.
        private ObservableCollection<TracertNode> pTracertNodes;
    
        // Keep the instance of the running tracert as a field, we need it.
        private Tracert pTracert;
    
        public bool IsTracertRunning
        {
            get { return this.pTracert != null; }
        }
    
        public ObservableCollection<TracertNode> DoTrace(string hostOrIP, int maxHops, int timeOut)
        {
            // If we are not already running a tracert...
            if (this.pTracert == null)
            {
                // Clear or creates the list of tracert nodes.
                if (this.pTracertNodes == null)
                    this.pTracertNodes = new ObservableCollection<TracertNode>();
                else
                    this.pTracertNodes.Clear();
    
                var tracert = new Tracert();
                tracert.HostNameOrAddress = hostOrIP;
                tracert.MaxHops = maxHops;
                tracert.TimeOut = timeOut;
    
                tracert.NewNodeFound += delegate(Tracert sender, TracertNode newNode)
                {
                    // This method is called inside Tracert thread.
                    // We need to use synchronization context to execute this method in our main window thread.
    
                    SynchronizationContext.Current.Post(delegate(object state)
                    {
                        // This method is called inside window thread.
                        this.OnTracertNodeFound(this.pTracertNodes, newNode);
                    }, null);
                };
    
                tracert.TracertCompleted += delegate(object sender, EventArgs e)
                {
                    // This method is called inside Tracert thread.
                    // We need to use synchronization context to execute this method in our main window thread.
    
                    SynchronizationContext.Current.Post(delegate(object state)
                    {
                        // This method is called inside window thread.
                        this.OnTracertCompleted();
                    }, null);
                };
    
                tracert.Trace();
    
                this.pTracert = tracert;
            }
    
            return this.pTracertNodes;
        }
    
        protected virtual void OnTracertCompleted()
        {
            // Remove tracert object,
            // we need this to let the garbage collector being able to release that objects.
            // We need also to allow another traceroute since the previous one completed.
            this.pTracert = null;
    
            System.Windows.MessageBox.Show("TraceRoute completed!");
        }
    
        protected virtual void OnTracertNodeFound(ObservableCollection<TracertNode> collection, TracertNode newNode)
        {
            // Add our tracert node.
            collection.Add(newNode);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.