2012-09-30 98 views
0

我希望我的申请将显示在我的表格中我的班级属性中,因此我开始使用BackgroundWorker创建班级并创建ProgressChanged仅使用BackgroundWorker报告仅第一次报告进度

我的课:

public class DumpFile 
{ 
    PacketDevice _device; 
    public int _packetsCount; 
    public double _bitsPerSecond; 
    public double _packetsPerSecond; 
    public DateTime _lastTimestamp; 
    public delegate void dlgPackProgress(int progress); 
    public event dlgPackProgress evePacketProgress; 

    public DumpFile(PacketDevice device, string pcapPath) 
    { 
     _device = device; 
     _pcapPath = pcapPath; 
     _packetsCount = 1; 
    } 

    public void startCapturing() 
    { 
OnPacketProgress(_packetsCount++); 

     using (PacketCommunicator communicator = _device.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) //open the device 
     { 
      ThreadStart starter = delegate { openAdapterForStatistics(_device); }; 
      new Thread(starter).Start(); 

      using (PacketDumpFile dumpFile = communicator.OpenDump(_pcapPath)) //open the dump file 
      { 
       communicator.ReceivePackets(0, dumpFile.Dump); //start the capture      
      } 
     } 
    } 
private void OnPacketProgress(int packet) 
{ 
    var handler = evePacketProgress; 
    if (handler != null) 
    { 
     handler(packet); 
    } 
} 

    public void openAdapterForStatistics(PacketDevice selectedOutputDevice) 
    { 
     using (PacketCommunicator statCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) //open the output adapter 
     { 
      ThreadStart start = delegate { test(selectedOutputDevice); }; 
      new Thread(start).Start(); 

      statCommunicator.SetFilter("tcp"); //compile and set the filter 
      statCommunicator.Mode = PacketCommunicatorMode.Statistics; //put the interface in statstics mode     
      statCommunicator.ReceiveStatistics(0, StatisticsHandler); 

     } 
    } 

    public void test(PacketDevice selectedOutputDevice) 
    { 
     using (PacketCommunicator communicator = selectedOutputDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) 
     { 
      communicator.ReceivePackets(0, PacketHandler); 
     } 
    } 

    private void PacketHandler(Packet packet) 
    { 
     string result = _packetsCount.ToString() + ". " + packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length; 
     _packetsCount++; 
    } 

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

启动按钮谁开始捕捉:

private void btnStartCapture_Click(object sender, EventArgs e) 
{ 
    timerSniffer.Start(); 
    btnStartTabSniffer.Enabled = false; 
    btnStopTabSniffer.Enabled = true; 
    groupBoxSelectTabSniffer.Enabled = false; 

    bgWorker = new BackgroundWorker(); 
    bgWorker.WorkerReportsProgress = true; 
    bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWSniffer_ProgressChanged); 
    bgWorker.DoWork += new DoWorkEventHandler(
     (s3, e3) => 
     { 
      DumpFile dumpFile = new DumpFile(deviceForCapturing, pcapFilePathSniffer); 

      tshark.evePacketProgress += new DumpFile.dlgPackProgress(
       (packet) => 
       { 
        bgWorker.ReportProgress(packet, dumpFile); 
       }); 

      dumpFile.startCapturing(); 
     }); 

    bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
     (s3, e3) => 
     { 
      groupBoxSelectTabSniffer.Enabled = true; 
     }); 

    bgWorker.RunWorkerAsync(); 
} 

ProgressChanged:

private void bgWSniffer_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    var dumpFile = (DumpFile)e.UserState; 
    lblNumberOfPacketsTabSniffer2.Text = dumpFile._packetsCount.ToString("#,##0"); 
    lblTrafficRateTabSniffer2.Text = (dumpFile._bitsPerSecond * 0.000001).ToString("0.##") + " Mbit/sec" + " (" + dumpFile._bitsPerSecond.ToString("#,##0") + " Bits/sec" + ")"; 
    lblPacketsRateTabSniffer2.Text = dumpFile._packetsPerSecond.ToString("#,##0") + " Packets/sec"; 
} 

的问题是,我的应用程序 “进入” ProgressChanged功能,但只在一次。

我想我在班上错过了一些东西。

回答

0

我只能找到一个电话OnPacketProgress(),它不在任何循环。

public void startCapturing() 
{ 
    OnPacketProgress(_packetsCount++); 
    .... 
} 

所以是的,那只会被调用一次。
你需要的东西ReceivePackets()

+0

感谢它的帮助! – falukky