仅首次使用BackgroundWorker报告报告进度

时间:2012-09-30 16:12:26

标签: c# winforms

我希望我的应用程序会在我的表单上显示我的类属性,因此我使用BackgroundWorker开始上课并创建ProgressChanged

我的班级:

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函数但只有一次。

我想我错过了班上的一些事情。

1 个答案:

答案 0 :(得分:0)

我只能找到一个OnPacketProgress()的来电,而且它不在任何循环中。

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

是的,那只会被召唤一次 你需要ReceivePackets()

里面的东西
相关问题