虽然计时器从未停止过,但它并未在第二次迭代中启动

时间:2013-06-09 13:43:22

标签: c# winforms

我有一些令人烦恼的错误,我找不到,并乐意为你提供帮助。

我有应用程序谁使用Wireshark文件(Pcap文件)并使用Pcap.Net project播放数据包 我只需将我的文件添加到Listbox并点击play button 实际播放数据包的类WiresharkFile 我用这种方式补充我的申请:

将我的所有文件添加到Listbox并单击播放另一个名为Job的类,在我的所有文件的构造函数List中收到,此类负责调用WiresharkFile并播放文件

这是我的Job班级:

using packetPlayer;
using PcapDotNet.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PacketPlayer.classes
{
    public class Job
    {
        private decimal _loopCount;
        private int _currentFileNum;
        private bool _shouldContinue;
        public bool shouldContinue
        {
            get { return _shouldContinue; }
            set
            {
                _shouldContinue = value;
                if (!_shouldContinue)
                {
                    foreach (WiresharkFile wf in wiresharkFileList)
                    {
                        wf.setIsStop(false);
                        wf.setStopButton(false);
                    }
                }
            }
        }
        private IEnumerable<string> _source;
        private PacketDevice _selectedOutputDevice;
        private double _speed;
        private int _parallelThreads;
        private decimal _loops;
        public CancellationTokenSource _tokenSource { get; set; }
        public delegate void StatusChangedDelegate(WiresharkFile wiresharkFile);
        public event StatusChangedDelegate statusChangedEvent;

        public Job(IEnumerable<string> source, PacketDevice selectedOutputDevice, double speed, int parallelThreads, decimal loops)
        {
            _source = source;
            _selectedOutputDevice = selectedOutputDevice;
            _speed = speed;
            _parallelThreads = parallelThreads;
            _loops = loops;
            _loopCount = 0;
        }

        public void doWork()
        {
            wiresharkFileList = new List<WiresharkFile>();
            _currentFileNum = 1;
            _shouldContinue = true;
            _tokenSource = new CancellationTokenSource();
            var token = _tokenSource.Token;
            Task.Factory.StartNew(() =>
            {
                try
                {
                    Parallel.ForEach(_source,
                        new ParallelOptions
                        {
                            MaxDegreeOfParallelism = _parallelThreads //limit number of parallel threads 
                        },
                        file =>
                        {
                            if (token.IsCancellationRequested)
                                return;
                            processFile(file, _selectedOutputDevice, _speed);
                            _currentFileNum++;
                        });
                }
                catch (Exception)
                { }

            }, _tokenSource.Token).ContinueWith(
                    t =>
                    {
                        _loopCount++;
                        if (continueAnotherLoop())
                            doWork();
                        else
                            OnFinishPlayEvent();
                    }
                , TaskScheduler.FromCurrentSynchronizationContext() //to ContinueWith (update UI) from UI thread
                );
        }

        private void processFile(string file, PacketDevice selectedOutputDevice, double speed)
        {
            Capinfos fileDetails = getFileDetails(file);
            WiresharkFile wiresharkFile = new WiresharkFile();
            wiresharkFileList.Add(wiresharkFile);
            wiresharkFile.startTimerEvent += wf_startTimerEvent;
            wiresharkFile.stopTimerEvent += wf_stopTimerEvent;
            wiresharkFile.statusChangedEvent += wf_statusChangedEvent;
            FileDetailsEvent(new FileInfo(file).Name, fileDetails.packets, fileDetails.duration);
            wiresharkFile.sendBuffer(file, selectedOutputDevice, speed, fileDetails.packets);
        }

        private Capinfos getFileDetails(string file)
        {
            Capinfos details = new Capinfos();
            details.readNumberOfPackets(file);
            details.readFileDuration(file);
            return details;
        }

        private void wf_statusChangedEvent(WiresharkFile wiresharkFile)
        {
            statusChangedEvent(wiresharkFile);
        }

        public int currentFileNum
        {
            get { return _currentFileNum; }
        }

        private bool continueAnotherLoop()
        {
            if (_loopCount < _loops)
                return true;
            else
                return false;
        }
    }
}

这个类有StatusChangedEvent传递给我的表单我当前的WiresharkFile对象,以便更新我的所有标签(WiresharkFile类有几个属性)

这是以每200毫秒启动计时器并更新我的GUI的形式的事件:

Timer timerPacket;

    private void job_startTimerEvent(WiresharkFile wiresharkFile)
    {
        timerPacket.Tag = wiresharkFile;

        if (InvokeRequired)
            this.Invoke((MethodInvoker)delegate
            {
                timerPacket.Start();
            });
        else
        {
            timerPacket.Start();
        }
    }

现在一切正常,我的所有标签都更新了,但我有一个奇怪的错误: 如果我选择几个文件一切正常但如果我选择例如1个文件并选择播放此文件两次,第一次迭代工作完美但在秒迭代我的计时器没有启动虽然我没有停止计时器

0 个答案:

没有答案