C# 计时器没有';t在第二次迭代中开始,尽管它从未停止过
我有一个讨厌的bug,我找不到,我很高兴能得到一些帮助 我的应用程序使用C# 计时器没有';t在第二次迭代中开始,尽管它从未停止过,c#,winforms,C#,Winforms,我有一个讨厌的bug,我找不到,我很高兴能得到一些帮助 我的应用程序使用Wireshark文件(Pcap文件)并使用Pcap.Net项目播放数据包 我只需将我的文件添加到我的列表框中,然后单击播放按钮 实际播放数据包调用的类WiresharkFile 我的应用程序是这样构建的: 将所有我的文件添加到我的列表框中后,单击播放另一个名为Job的类,该类在所有我的文件的构造函数列表中接收,该类负责调用WiresharkFile并播放文件 这是我的工作课程: using packetPlayer; us
Wireshark
文件(Pcap文件)并使用Pcap.Net项目播放数据包
我只需将我的文件添加到我的列表框中
,然后单击播放按钮
实际播放数据包调用的类WiresharkFile
我的应用程序是这样构建的:
将所有我的文件添加到我的列表框中后,单击播放另一个名为Job
的类,该类在所有我的文件的构造函数列表中接收,该类负责调用WiresharkFile
并播放文件
这是我的工作
课程:
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;
}
}
}
现在一切正常,我所有的标签都更新了,但我有一个奇怪的错误:
如果我选择几个文件,一切正常,但如果我选择例如1个文件,并选择播放此文件两次,则第一次迭代工作正常,但在第二次迭代中,我的计时器没有启动,尽管我没有停止计时器我很难理解应用程序架构。如果这个问题是关于计时器错误的,您应该多说一些关于计时器调用的内容,少说一些甚至不涉及的作业类。例如,我看不到您在何处以及如何创建timerPacket
实例,也不清楚为什么要启动/重新启动计时器,或者如果需要停止计时器,为什么不停止计时器。这需要一个调试器,只有您有一个调试器可以调试此代码。首先从代码中删除catch{},这只会使代码无法以完全不可诊断的方式运行。
Timer timerPacket;
private void job_startTimerEvent(WiresharkFile wiresharkFile)
{
timerPacket.Tag = wiresharkFile;
if (InvokeRequired)
this.Invoke((MethodInvoker)delegate
{
timerPacket.Start();
});
else
{
timerPacket.Start();
}
}