77using System . ComponentModel ;
88using System . Runtime . CompilerServices ;
99using BlazorBootstrap ;
10+ using System . Threading ;
1011
1112namespace TelegramDownloader . Models
1213{
1314
1415 public class InfoDownloadTaksModel
1516 {
17+ public Mutex mutex = new Mutex ( ) ;
1618 public event EventHandler < InfoTaskEventArgs > EventChanged ;
1719 public StateTask state { get ; set ; } = StateTask . Pending ;
1820 public DateTime creationDate { get ; set ; } = DateTime . Now ;
@@ -38,10 +40,11 @@ public async void AddOne(long size)
3840 {
3941 if ( state == StateTask . Canceled )
4042 cancelTask ( ) ;
41-
43+ mutex . WaitOne ( ) ;
4244 executed += 1 ;
4345 progress = Convert . ToInt32 ( executed * 100 / total ) ;
4446 executedSize += size ;
47+ mutex . ReleaseMutex ( ) ;
4548 EventChanged ? . Invoke ( this , new InfoTaskEventArgs ( ) ) ;
4649
4750 }
@@ -66,16 +69,20 @@ public void addUpload(UploadModel um)
6669 {
6770 if ( state == StateTask . Canceled )
6871 return ;
72+ mutex . WaitOne ( ) ;
6973 deleteNotWorkingTasks ( ) ;
7074 currentUploads . Add ( um ) ;
75+ mutex . ReleaseMutex ( ) ;
7176 }
7277
7378 public void addDownloads ( DownloadModel dm )
7479 {
7580 if ( state == StateTask . Canceled )
7681 return ;
82+ mutex . WaitOne ( ) ;
7783 deleteNotWorkingTasks ( ) ;
7884 currentDownloads . Add ( dm ) ;
85+ mutex . ReleaseMutex ( ) ;
7986 }
8087
8188 public void Retry ( )
@@ -159,6 +166,7 @@ public void ProgressCallback(long transmitted, long totalSize)
159166 throw new Exception ( $ "Paused { name } ") ;
160167 }
161168 tis . addDownloadBytes ( transmitted - _transmitted ) ;
169+ mutex . WaitOne ( ) ;
162170 _transmitted = transmitted ;
163171 _sizeString = HelperService . SizeSuffix ( totalSize ) ;
164172 _transmittedString = HelperService . SizeSuffix ( transmitted ) ;
@@ -172,18 +180,23 @@ public void ProgressCallback(long transmitted, long totalSize)
172180 nm . sendEvent ( new Notification ( $ "Download { name } completed", "Download Completed" , NotificationTypes . Success ) ) ;
173181 tis . CheckPendingDownloads ( ) ;
174182 }
183+ mutex . ReleaseMutex ( ) ;
175184 }
176185
177186 public void Cancel ( )
178187 {
188+ mutex . WaitOne ( ) ;
179189 state = StateTask . Canceled ;
190+ mutex . ReleaseMutex ( ) ;
180191 tis . CheckPendingDownloads ( ) ;
181192 EventChanged ? . Invoke ( this , new DownloadEventArgs ( ) ) ;
182193 }
183194
184195 public void Pause ( )
185196 {
197+ mutex . WaitOne ( ) ;
186198 state = StateTask . Paused ;
199+ mutex . ReleaseMutex ( ) ;
187200 EventChanged ? . Invoke ( this , new DownloadEventArgs ( ) ) ;
188201 }
189202
@@ -195,6 +208,7 @@ public void RetryCallback()
195208
196209 public class UploadModel
197210 {
211+ public Mutex mutex = new Mutex ( ) ;
198212 public event EventHandler < UploadEventArgs > EventChanged ;
199213 public Guid id = Guid . NewGuid ( ) ;
200214 public string action { get ; set ; } = "Upload" ;
@@ -219,6 +233,7 @@ public virtual void ProgressCallback(long transmitted, long totalSize)
219233 if ( state == StateTask . Canceled )
220234 throw new Exception ( $ "Canceled { name } ") ;
221235 tis . addUploadBytes ( transmitted - _transmitted ) ;
236+ mutex . WaitOne ( ) ;
222237 _transmitted = transmitted ;
223238 _sizeString = HelperService . SizeSuffix ( totalSize ) ;
224239 _transmittedString = HelperService . SizeSuffix ( transmitted ) ;
@@ -230,6 +245,7 @@ public virtual void ProgressCallback(long transmitted, long totalSize)
230245 NotificationModel nm = new NotificationModel ( ) ;
231246 nm . sendEvent ( new Notification ( $ "Upload { name } completed", "Upload Completed" , NotificationTypes . Success ) ) ;
232247 }
248+ mutex . ReleaseMutex ( ) ;
233249
234250 }
235251
@@ -240,13 +256,17 @@ public void SendNotification()
240256
241257 public void SetState ( StateTask newState )
242258 {
259+ mutex . WaitOne ( ) ;
243260 state = newState ;
261+ mutex . ReleaseMutex ( ) ;
244262 EventChanged ? . Invoke ( this , new UploadEventArgs ( ) ) ;
245263 }
246264
247265 public void Cancel ( )
248266 {
267+ mutex . WaitOne ( ) ;
249268 state = StateTask . Canceled ;
269+ mutex . ReleaseMutex ( ) ;
250270 EventChanged ? . Invoke ( this , new UploadEventArgs ( ) ) ;
251271 }
252272
@@ -264,6 +284,7 @@ public SplitModel(): base()
264284 }
265285 public override void ProgressCallback ( long transmitted , long totalSize )
266286 {
287+ mutex . WaitOne ( ) ;
267288 _transmitted = transmitted ;
268289 _sizeString = HelperService . SizeSuffix ( totalSize ) ;
269290 _transmittedString = HelperService . SizeSuffix ( transmitted ) ;
@@ -275,7 +296,7 @@ public override void ProgressCallback(long transmitted, long totalSize)
275296 NotificationModel nm = new NotificationModel ( ) ;
276297 nm . sendEvent ( new Notification ( $ "Split { name } completed", "Split Completed" , NotificationTypes . Success ) ) ;
277298 }
278-
299+ mutex . ReleaseMutex ( ) ;
279300 }
280301 }
281302
@@ -287,15 +308,19 @@ public Md5Model() : base()
287308 }
288309 public virtual void Init ( long size , string filename )
289310 {
311+ mutex . WaitOne ( ) ;
290312 _sizeString = HelperService . SizeSuffix ( size ) ;
291313 name = filename ;
314+ mutex . ReleaseMutex ( ) ;
292315 base . InvokeEvent ( new UploadEventArgs ( ) ) ;
293316 }
294317
295318 public virtual void Finish ( )
296319 {
320+ mutex . WaitOne ( ) ;
297321 state = StateTask . Completed ;
298322 progress = 100 ;
323+ mutex . ReleaseMutex ( ) ;
299324 base . InvokeEvent ( new UploadEventArgs ( ) ) ;
300325 }
301326 }
0 commit comments