2

我正在 WPF 中实现 Tracert 的可视化版本(作为学习练习),其中结果进入列表框。问题是 (1) 绑定到 tracertDataView 的列表框没有更新,但是 (2) 我的整个应用程序挂起。

我确定#2 是线程问题,但我不确定如何纠正它(以正确的方式)。此外,我不确定我更新/绑定“DoTrace”结果的技术是否正确。

这是我在App.xaml中的数据源

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

</Window.Resources>

应用程序.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; }
    }
}

这是主窗口代码

  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);
    }
}

仅供参考 实例对象“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;
    }
4

3 回答 3

2

我不明白你是如何使用 AutoResetEvent 的,我想它不应该以这种方式使用 :)

但是由于 Trace 已经在另一个线程中运行,你确定在你的 Tracert 类中没有事件“OnTracertComplete”或类似的东西吗?

如果没有,为什么不将 DispatchTimer 放入应用程序中?该计时器将定期轮询,直到 tracert.IsDone 变为真。如果在操作完成之前阻止应用程序线程的执行,则会阻止窗口事件循环的执行,因此窗口将永远不会被更新。

另一件重要的事情:你不能从另一个线程更新 ObservableCollections。请注意并确保 WPF 窗口中更新的所有内容都是从窗口的同一线程执行的。不知道您的 Trace 类究竟做了什么,但您的问题似乎当然是等待循环,这在 GUI 应用程序中没有意义。

使用通知事件或计时器来轮询结果。对于这个特定的实现,具有 1 秒分辨率的计时器对我来说似乎很好,并且性能影响绝对是最小的。

如果您能够修改 Tracert 类,这是一个可能的实现。

    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);
        }

    }

这是运行 tracert 的代码,这是 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);
    }
于 2011-09-01T16:24:30.520 回答
1

问题是不仅列表框没有更新,而且我的整个应用程序都挂起。

这可能是由于AutoResetEvent. DoTrace您明确调用Wait.WaitOne();事件句柄,但据我所知,从来没有Set()。这将导致应用程序在您调用Wait.WaitOne().

听起来像是tracert.Trace()一种异步方法。它是否包含某种形式的回调/事件以在完成时通知您?如果是这样,您应该使用它而不是循环轮询来确定它何时完成。

于 2011-09-01T16:23:57.477 回答
1

(1)tracertDataView绑定的listbox没有更新

您不会看到列表框的更新,因为您正在为 TracertResultNodes 属性分配一个新集合,在这种情况下绑定根本不起作用,因为分配了一个新集合。

除了确保集合在下面 Salvatore 概述的同一线程中更新之外,您应该只从现有集合中添加或删除项目,而不是分配由 DoTrace 函数生成的新项目。

private void DoTrace_Click(object sender, RoutedEventArgs e)
    {
       foreach(var traceNode in _tracertWrapper.Results)
       {
          ((App)Application.Current).TracertResultNodes.Add(traceNode);
       }

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

如果您确实分配了一个新的,那么您需要在您的 App 类上实现 INotifyPropertyChanged,但我不确定它如何(或是否)起作用(我之前没有尝试过)。

于 2011-09-01T16:38:36.030 回答