/// <summary> /// Creates a new instance of the <see cref="BandwidthAnalyzer"/> class. /// </summary> public BandwidthAnalyzer() { _resultData = new BandwidthAnalizerResult(); InterfaceSelector = new InterfaceSelector(); _startAnalysisCommand = new Command(StartAnalysis, CanStartAnalysis); _stopAnalysisCommand = new Command(StopAnalysis, CanStopAnalysis); // hook on interfaces change for command refresh InterfaceSelector.InterfacesChanged += (sender, e) => { UpdateCommands(); }; // just do the initial interface initialization InterfaceSelector.RefreshInterfaces(); }
/// <summary> /// Prepares everything to start the analysis /// </summary> private void StartAnalysis() { // create cancellation token _cancellationTokenSource = new CancellationTokenSource(); // create SynchronizationContext SynchronizationContext synchronizationContext = SynchronizationContext.Current; // set working Working = true; // set empty ResultData = new BandwidthAnalizerResult(); // start ping in interval _workingTask = Repeat.Interval(TimeSpan.FromSeconds(2), () => { StartAnalysisInternal(synchronizationContext); }, _cancellationTokenSource.Token); // continue with task setting falg to IsRunning false to know when task stoped _workingTask = _workingTask.ContinueWith((previousTask) => { if (previousTask.Status != TaskStatus.RanToCompletion) { Working = false; } }, TaskScheduler.FromCurrentSynchronizationContext()); }
/// <summary> /// Does the analysis /// </summary> private void StartAnalysisInternal(SynchronizationContext synchronizationContext) { // get data IPv4InterfaceStatistics interfaceStatistics = GetBandwidthData(); // create result BandwidthAnalizerResult bandwidthAnalizerResult = null; //// calculate speed //if (_bytesReceived != 0 && _bytesSent != 0) { // //int bytesReceivedSpeed = (int) (_bytesReceived - interfaceStatistics.BytesReceived) / 1024 / 1024; // //int bytesSentSpeed = (int) (_bytesSent - interfaceStatistics.BytesSent) / 1024 / 1024; // // create result // bandwidthAnalizerResult = new BandwidthAnalizerResult(bytesSentSpeed, bytesReceivedSpeed); //} //else { // // create result // bandwidthAnalizerResult = new BandwidthAnalizerResult(); //} //// remember current values //_bytesReceived = interfaceStatistics.BytesReceived; //_bytesSent = interfaceStatistics.BytesSent; int bytesReceivedSpeed = (int)(interfaceStatistics.BytesReceived) / 1024 / 1024; int bytesSentSpeed = (int)(interfaceStatistics.BytesSent) / 1024 / 1024; // create result bandwidthAnalizerResult = new BandwidthAnalizerResult(bytesSentSpeed, bytesReceivedSpeed); // set result synchronizationContext.Send((objState) => { ResultData = bandwidthAnalizerResult; }, synchronizationContext); }