【发布时间】:2015-09-18 18:08:47
【问题描述】:
我有带有PcapDotNet DLL 的WPF 应用程序可以测量我的机器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)但在第二个接口中它总是为零(我让一定会通过这个接口产生一些流量,所以它不应该是零)
【问题讨论】:
-
您的编辑是一个不同的问题,也许应该这样做,因为它不再与类成员是否为静态有关(这就是问题标题的含义)。
-
另外...我看不出有什么奇怪的地方。您是否调试过您的应用程序并确保正在调用
StatisticsHandler并实际更新第二个接口中的值?也许它是零,即使它不应该是。 -
你是说我的代码现在打开了我所有的界面,这看起来不错?
标签: wpf multithreading pcap.net