Skip to content

Commit

Permalink
Make present download sotre thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
aurimasc committed Nov 19, 2019
1 parent 2b1b19c commit 7299ccb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
45 changes: 27 additions & 18 deletions BackgroundDownload/BackgroundDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ public static BackgroundDownload[] backgroundDownloads
{
get
{
LoadDownloads();
var ret = new BackgroundDownload[_downloads.Count];
int i = 0;
foreach (var download in _downloads)
ret[i++] = download.Value;
return ret;
lock (typeof(BackgroundDownload))
{
LoadDownloads();
var ret = new BackgroundDownload[_downloads.Count];
int i = 0;
foreach (var download in _downloads)
ret[i++] = download.Value;
return ret;
}
}
}

Expand All @@ -90,13 +93,16 @@ public static BackgroundDownload Start(Uri url, String filePath)

public static BackgroundDownload Start(BackgroundDownloadConfig config)
{
LoadDownloads();
if (_downloads.ContainsKey(config.filePath))
throw new ArgumentException("Download of this file is already present");
var download = new BackgroundDownloadimpl(config);
_downloads.Add(config.filePath, download);
SaveDownloads();
return download;
lock (typeof(BackgroundDownload))
{
LoadDownloads();
if (_downloads.ContainsKey(config.filePath))
throw new ArgumentException("Download of this file is already present");
var download = new BackgroundDownloadimpl(config);
_downloads.Add(config.filePath, download);
SaveDownloads();
return download;
}
}

protected BackgroundDownload()
Expand Down Expand Up @@ -131,12 +137,15 @@ protected BackgroundDownload(BackgroundDownloadConfig config)

public virtual void Dispose()
{
_downloads.Remove(_config.filePath);
SaveDownloads();
if (_status == BackgroundDownloadStatus.Downloading)
lock (typeof(BackgroundDownload))
{
_status = BackgroundDownloadStatus.Failed;
_error = "Aborted";
_downloads.Remove(_config.filePath);
SaveDownloads();
if (_status == BackgroundDownloadStatus.Downloading)
{
_status = BackgroundDownloadStatus.Failed;
_error = "Aborted";
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions BackgroundDownload/BackgroundDownloadAndroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public Callback()

void downloadCompleted()
{
foreach (var download in _downloads.Values)
((BackgroundDownloadAndroid)download).CheckFinished();
lock (typeof(BackgroundDownload))
{
foreach (var download in _downloads.Values)
((BackgroundDownloadAndroid) download).CheckFinished();
}
}
}

Expand Down

0 comments on commit 7299ccb

Please sign in to comment.