定义为静态或非静态时,类成员的行为会有所不同

时间:2015-07-01 14:11:35

标签: wpf multithreading pcap.net

我有WPF个应用程序,其PcapDotNet DLL用于衡量我的计算机Interface Rate

这是Model

public class Interface
{
    public PacketDevice PacketDevice { get { return livePacketDevice; } }
    private DateTime _lastTimestamp;
    private double _bitsPerSecond;
    private double _packetsPerSecond;
    private DateTime _lastTimestamp;
    private static List<Interface> _machineInterfaces; // list of all machine interfaces

    public void Start(Interface inf)
    {
        OpenAdapterForStatistics(inf.PacketDevice);
    }

    public void OpenAdapterForStatistics(PacketDevice selectedOutputDevice)
    {
        if (selectedOutputDevice != null)
        {
            using (PacketCommunicator statCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) //open the output adapter
            {
                try
                {
                    statCommunicator.Mode = PacketCommunicatorMode.Statistics; //put the interface in statstics mode
                    statCommunicator.ReceiveStatistics(0, StatisticsHandler); //start the main loop
                }
                catch (Exception)
                { }
            }
        }
    }

    private void StatisticsHandler(PacketSampleStatistics statistics)
    {
        DateTime currentTimestamp = statistics.Timestamp; //current sample time
        DateTime previousTimestamp = _lastTimestamp; //previous sample time
        _lastTimestamp = currentTimestamp; //set _lastTimestamp for the next iteration

        if (previousTimestamp == DateTime.MinValue) //if there wasn't a previous sample than skip this iteration (it's the first iteration)
            return;

        double delayInSeconds = (currentTimestamp - previousTimestamp).TotalSeconds; //calculate the delay from the last sample
        _bitsPerSecond = statistics.AcceptedBytes * 8 / delayInSeconds; //calculate bits per second
        _packetsPerSecond = statistics.AcceptedPackets / delayInSeconds; //calculate packets per second

        if (NewPointEventHandler != null)
            NewPointEventHandler(_bitsPerSecond);
        double value = _packetsPerSecond;
    }

您可以看到Start方法开始衡量Interface费率并将值放入2个字段:

_bitsPerSecond_packetsPerSecond

因此,在应用程序启动后,我有了这个字段:

List<Interface> _machineInterfaces; 

读取我的所有机器接口。

之后我开始使用Start方法:

    private void StartStatistics()
    {
        int index = listview.SelectedIndex; // select the selected interface from my `ListView` list.
        Interface inf = new Interface();
        ThreadStart tStarter = delegate
        {              
            inf.Start(Interface.MachineInterfaces[index]); // send the selected interface
        };
        Thread thread = new Thread(tStarter);
        thread.IsBackground = true;
        thread.Start();

        statisticsTimer.Start(); // start my timer
    }
  • 好的,现在这是我的问题:

这是我的Timer Tick Event

public RadObservableCollection<double> mbitPerSecondValue { get; private set; }

如果我的BitsPerSecond Class Interface member定义为常规而非Static,则其值始终为零:

    private void statisticsTimer_Tick(object sender, EventArgs e)
    {
        int index = listview.SelectedIndex;

        double bps = Interface.MachineInterfaces[index].BitsPerSecond; // always zero !
        mbitPerSecondValue.Add(bps);
    }

如果BitsPerSecond定义为静态所有好处:

    private void statisticsTimer_Tick(object sender, EventArgs e)
    {
        int index = listview.SelectedIndex;
        double bps = Interface.BitsPerSecond;
        mbitPerSecondValue.Add(bps);
    }

所以我的问题是为什么?

修改

目前我改变了我的职能:

private void StartStatistics()
        {
            int index = lvAdapters.SelectedIndex;
            Interface inf = new Interface();
            ThreadStart tStarter = delegate
            {
                foreach (Interface item in Interface.MachineInterfaces)
                    item.Start();
            };

            Thread thread = new Thread(tStarter);
            thread.IsBackground = true;
            thread.Start();

            statisticsTimer.Start();
        }

我想要实现的是打开我机器上每个界面的统计数据,但是再次在第一个界面中(我有2个)我可以看到流量变化(BitsPerSecond)但在第二个界面中它始终为零(我制作)一定要通过这个界面产生一些流量,所以它不应该是零)

2 个答案:

答案 0 :(得分:2)

对于第二个问题,尝试从不同的线程调用每个接口的Start。我看到的唯一可疑的事情是,statCommunicator.ReceiveStatistics可能会阻塞线程并阻止其他接口启动。

这应该避免这个问题:

private void StartStatistics()
{
    foreach (Interface item in Interface.MachineInterfaces)
    {
        ThreadStart tStarter = delegate
        {
            item.Start();
        };

        Thread thread = new Thread(tStarter);
        thread.IsBackground = true;
        thread.Start();
    }

    statisticsTimer.Start();
}

答案 1 :(得分:0)

嗯,很明显为什么它在定义为static时有效:Interface的所有实例共享相同的属性,所以当你从一个地方增加它的值时,新值会自动在任何地方都可用

但作为常规非静态属性,您必须确保从先前修改过的同一实例中读取。你不是。

首先,您正在创建一个新的Interface(让我们称之为接口A),然后调用它的Start,传递另一个Interface(我们称之为接口B) )你从Interface.MachineInterfaces得到的参数:

private void StartStatistics()
{
    ...
    Interface inf = new Interface();
    ThreadStart tStarter = delegate
    {              
        inf.Start(Interface.MachineInterfaces[index]); // send the selected interface
    };
    ...
}

在接口A的Start方法中,您正在订阅接口B的统计信息,但处理程序仍在接口A中:

public void Start(Interface inf)
{
    OpenAdapterForStatistics(inf.PacketDevice);
}

public void OpenAdapterForStatistics(PacketDevice selectedOutputDevice)
{
    ...
    statCommunicator.ReceiveStatistics(0, StatisticsHandler); //start the main loop
    ...
}

当调用接口A中的处理程序时,它会增加自己的_bitsPerSecond值。不是接口B,而是接口A。

private void StatisticsHandler(PacketSampleStatistics statistics)
{
    ...
    _bitsPerSecond = statistics.AcceptedBytes * 8 / delayInSeconds; //calculate bits per second
    ...
}

但最后,您正在检查界面B中BitsPerSecond的值,再次从Interface.MachineInterfaces中获取!

private void statisticsTimer_Tick(object sender, EventArgs e)
{
    ...
    double bps = Interface.MachineInterfaces[index].BitsPerSecond; // always zero !
    ...
}

- 建议的解决方案1 ​​ -

为什么不这样做才Start使用自己的实例,所以您不必创建新的Interface只是为了使用它?

public void Start()
{
    OpenAdapterForStatistics(this.PacketDevice);
}

你可以这样做:

private void StartStatistics()
{
    int index = listview.SelectedIndex; // select the selected interface from my `ListView` list.
    ThreadStart tStarter = delegate
    {              
        Interface.MachineInterfaces[index].Start(); // send the selected interface
    };
    Thread thread = new Thread(tStarter);
    thread.IsBackground = true;
    thread.Start();

    statisticsTimer.Start(); // start my timer
}

...您应该在Timer Tick回调中获得所需的输出。

- 建议的解决方案2 -

如果您不想从Start内的原始接口调用Interface.MachineInterfaces,那么您必须将新接口存储在某种字典中,以便稍后可以访问它从中获取BitsPerSecond

private Dictionary<Interface, Interface> InterfaceDictionary = new Dictionary<Interface, Interface>();

private void StartStatistics()
{
    int index = listview.SelectedIndex; // select the selected interface from my `ListView` list.
    Interface inf = new Interface();
    ThreadStart tStarter = delegate
    {              
        inf.Start(Interface.MachineInterfaces[index]); // send the selected interface
    };
    Thread thread = new Thread(tStarter);
    thread.IsBackground = true;
    thread.Start();

    statisticsTimer.Start(); // start my timer

    if (InterfaceDictionary.ContainsKey(Interface.MachineInterfaces[index]))
        InterfaceDictionary[Interface.MachineInterfaces[index]] = inf;
    else
        InterfaceDictionary.Add(Interface.MachineInterfaces[index], inf);
}

在您的Timer Tick回调中,从相关接口获取数据,而不是Interface.MachineInterfaces中的数据:

private void statisticsTimer_Tick(object sender, EventArgs e)
{
    int index = listview.SelectedIndex;
    var interface = InterfaceDictionary[Interface.MachineInterfaces[index]];
    double bps = interface.BitsPerSecond;
    mbitPerSecondValue.Add(bps);
}