@@ -18,7 +18,7 @@ public partial class TrafficStatisticsForm : Form
1818
1919 private IRelay relay ;
2020 private Traffic rawTrafficStatistics = new Traffic ( ) ;
21- private LinkedList < TrafficLog > trafficLogList ;
21+ private LinkedList < TrafficLog > trafficLogList = new LinkedList < TrafficLog > ( ) ;
2222
2323 public TrafficStatisticsForm ( )
2424 {
@@ -86,6 +86,7 @@ private void TopMostheckBox_CheckedChanged(object sender, EventArgs e)
8686 private void ResetButton_Click ( object sender , EventArgs e )
8787 {
8888 rawTrafficStatistics . reset ( ) ;
89+ InitTrafficHistoricalList ( ) ;
8990 }
9091
9192 private void timer1_Tick ( object sender , EventArgs e )
@@ -97,6 +98,7 @@ private void timer2_Tick(object sender, EventArgs e)
9798 {
9899 try
99100 {
101+ timer2 . Stop ( ) ;
100102 UpdateTrafficList ( ) ;
101103 if ( TrafficChart . InvokeRequired )
102104 {
@@ -114,6 +116,10 @@ private void timer2_Tick(object sender, EventArgs e)
114116 {
115117 string s = ex . Message ;
116118 }
119+ finally
120+ {
121+ timer2 . Start ( ) ;
122+ }
117123 }
118124
119125 private void CleanLogsButton_Click ( object sender , EventArgs e )
@@ -197,27 +203,30 @@ private void AppendLog(string line)
197203
198204 private void InitTrafficHistoricalList ( )
199205 {
200- if ( trafficLogList == null )
201- trafficLogList = new LinkedList < TrafficLog > ( ) ;
202- else
203- trafficLogList . Clear ( ) ;
204- for ( int i = 0 ; i < trafficLogSize ; i ++ )
206+ lock ( trafficLogList )
205207 {
206- trafficLogList . AddLast ( new TrafficLog ( ) ) ;
208+ trafficLogList . Clear ( ) ;
209+ for ( int i = 0 ; i < trafficLogSize ; i ++ )
210+ {
211+ trafficLogList . AddLast ( new TrafficLog ( ) ) ;
212+ }
207213 }
208214 }
209215
210216 private void UpdateTrafficList ( )
211217 {
212- TrafficLog previous = trafficLogList . Last . Value ;
213- TrafficLog current = new TrafficLog (
214- new Traffic ( rawTrafficStatistics ) ,
215- new Traffic ( rawTrafficStatistics , previous . raw )
216- ) ;
217- trafficLogList . AddLast ( current ) ;
218-
219- while ( trafficLogList . Count > trafficLogSize ) trafficLogList . RemoveFirst ( ) ;
220- while ( trafficLogList . Count < trafficLogSize ) trafficLogList . AddFirst ( new TrafficLog ( ) ) ;
218+ lock ( trafficLogList )
219+ {
220+ TrafficLog previous = trafficLogList . Last . Value ;
221+ TrafficLog current = new TrafficLog (
222+ new Traffic ( rawTrafficStatistics ) ,
223+ new Traffic ( rawTrafficStatistics , previous . raw )
224+ ) ;
225+ trafficLogList . AddLast ( current ) ;
226+
227+ while ( trafficLogList . Count > trafficLogSize ) trafficLogList . RemoveFirst ( ) ;
228+ while ( trafficLogList . Count < trafficLogSize ) trafficLogList . AddFirst ( new TrafficLog ( ) ) ;
229+ }
221230 }
222231
223232 private List < float > rawInboundPoints = new List < float > ( ) ;
@@ -226,21 +235,25 @@ private void UpdateTrafficList()
226235
227236 private void UpdateTrafficChart ( )
228237 {
238+ TrafficLog last ;
229239 long maxSpeedValue = 0 ;
230240 rawInboundPoints . Clear ( ) ;
231241 rawOutboundPoints . Clear ( ) ;
232- foreach ( TrafficLog item in trafficLogList )
242+ lock ( trafficLogList )
233243 {
234- rawInboundPoints . Add ( item . rawSpeed . inbound ) ;
235- rawOutboundPoints . Add ( item . rawSpeed . outbound ) ;
244+ last = trafficLogList . Last . Value ;
245+ foreach ( TrafficLog item in trafficLogList )
246+ {
247+ rawInboundPoints . Add ( item . rawSpeed . inbound ) ;
248+ rawOutboundPoints . Add ( item . rawSpeed . outbound ) ;
236249
237- maxSpeedValue = Math . Max ( maxSpeedValue ,
238- Math . Max ( item . rawSpeed . inbound , item . rawSpeed . outbound )
239- ) ;
250+ maxSpeedValue = Math . Max ( maxSpeedValue ,
251+ Math . Max ( item . rawSpeed . inbound , item . rawSpeed . outbound )
252+ ) ;
253+ }
240254 }
241255
242256 FormattedSize maxSpeed = new FormattedSize ( maxSpeedValue ) ;
243- TrafficLog last = trafficLogList . Last . Value ;
244257 RawInboundSpeed . Text = new FormattedSize ( last . rawSpeed . inbound ) + "/s" ;
245258 RawOutboundSpeed . Text = new FormattedSize ( last . rawSpeed . outbound ) + "/s" ;
246259
0 commit comments