@@ -25,6 +25,7 @@ public class TransactionInfoService
2525 public long bytesDownloaded = 0 ;
2626 public List < SpeedHistory > downloadSpeedsHistory = new List < SpeedHistory > ( ) ;
2727 public List < SpeedHistory > uploadSpeedsHistory = new List < SpeedHistory > ( ) ;
28+ private readonly object _speedHistoryLock = new object ( ) ;
2829 private int speedHistoryCount = 0 ;
2930
3031 private static Mutex PendingDownloadMutex = new Mutex ( ) ;
@@ -123,22 +124,44 @@ private void OnTimedEvent(Object source, ElapsedEventArgs e)
123124
124125 private bool canStopTimer ( )
125126 {
126- return uploadSpeedsHistory . Sum ( x => x . speed ) + downloadSpeedsHistory . Sum ( x => x . speed ) == 0 ;
127+ lock ( _speedHistoryLock )
128+ {
129+ return uploadSpeedsHistory . Sum ( x => x . speed ) + downloadSpeedsHistory . Sum ( x => x . speed ) == 0 ;
130+ }
127131 }
128132
129133 private void setHistorySpeed ( )
130134 {
131135 DateTime now = DateTime . Now ;
132136 var activeDownloads = downloadModels . Where ( x => x . state == StateTask . Working ) . Select ( x => x . name ) . ToList ( ) ;
133137 var activeUploads = uploadModels . Where ( x => x . state == StateTask . Working ) . Select ( x => x . name ) . ToList ( ) ;
134- downloadSpeedsHistory . Add ( new SpeedHistory ( ) { time = now , speed = bytesDownloaded , speedString = downloadSpeed , activeFiles = activeDownloads } ) ;
135- uploadSpeedsHistory . Add ( new SpeedHistory ( ) { time = now , speed = bytesUploaded , speedString = uploadSpeed , activeFiles = activeUploads } ) ;
136- downloadSpeedsHistory . RemoveAll ( x => ( now - x . time ) . TotalSeconds > MAX_SPEED_HISTORY_SECONDS ) ;
137- uploadSpeedsHistory . RemoveAll ( x => ( now - x . time ) . TotalSeconds > MAX_SPEED_HISTORY_SECONDS ) ;
138+ lock ( _speedHistoryLock )
139+ {
140+ downloadSpeedsHistory . Add ( new SpeedHistory ( ) { time = now , speed = bytesDownloaded , speedString = downloadSpeed , activeFiles = activeDownloads } ) ;
141+ uploadSpeedsHistory . Add ( new SpeedHistory ( ) { time = now , speed = bytesUploaded , speedString = uploadSpeed , activeFiles = activeUploads } ) ;
142+ downloadSpeedsHistory . RemoveAll ( x => ( now - x . time ) . TotalSeconds > MAX_SPEED_HISTORY_SECONDS ) ;
143+ uploadSpeedsHistory . RemoveAll ( x => ( now - x . time ) . TotalSeconds > MAX_SPEED_HISTORY_SECONDS ) ;
144+ }
138145 if ( HistorykEventChanged != null )
139146 HistorykEventChanged . Invoke ( this , EventArgs . Empty ) ;
140147 }
141148
149+ public List < SpeedHistory > GetDownloadSpeedsHistoryCopy ( )
150+ {
151+ lock ( _speedHistoryLock )
152+ {
153+ return downloadSpeedsHistory . ToList ( ) ;
154+ }
155+ }
156+
157+ public List < SpeedHistory > GetUploadSpeedsHistoryCopy ( )
158+ {
159+ lock ( _speedHistoryLock )
160+ {
161+ return uploadSpeedsHistory . ToList ( ) ;
162+ }
163+ }
164+
142165 public async Task addDownloadBytes ( long bytes )
143166 {
144167 DownloadBytesMutex . WaitOne ( ) ;
0 commit comments