下载任务数并发控制(有一点点bug)
parent
cf09403486
commit
a6a8c0a89b
34
App.xaml.cs
34
App.xaml.cs
|
|
@ -25,9 +25,11 @@ namespace HeBianGu.App.Disk
|
||||||
{
|
{
|
||||||
public new static ComponentResourceKey Custom
|
public new static ComponentResourceKey Custom
|
||||||
{
|
{
|
||||||
get => new ComponentResourceKey(typeof (LinkWindowBase), (object) "S.Window.Link.Custom");
|
get => new ComponentResourceKey(typeof(LinkWindowBase), (object)"S.Window.Link.Custom");
|
||||||
}
|
}
|
||||||
public static ComponentResourceKey WaitingPercent => new ComponentResourceKey(typeof(ProgressBarKeys), "S.ProgressBar.Custom");
|
|
||||||
|
public static ComponentResourceKey WaitingPercent =>
|
||||||
|
new ComponentResourceKey(typeof(ProgressBarKeys), "S.ProgressBar.Custom");
|
||||||
|
|
||||||
protected override MainWindowBase CreateMainWindow(StartupEventArgs e)
|
protected override MainWindowBase CreateMainWindow(StartupEventArgs e)
|
||||||
{
|
{
|
||||||
|
|
@ -137,11 +139,12 @@ namespace HeBianGu.App.Disk
|
||||||
{
|
{
|
||||||
var tempSetting = new SystemSetting();
|
var tempSetting = new SystemSetting();
|
||||||
tempSetting.TaskCount = 3;
|
tempSetting.TaskCount = 3;
|
||||||
tempSetting.SyncDir = Path.Combine(SelectDrive(),"HopetryBoxData");
|
tempSetting.SyncDir = Path.Combine(SelectDrive(), "HopetryBoxData");
|
||||||
if (!Directory.Exists(tempSetting.SyncDir))
|
if (!Directory.Exists(tempSetting.SyncDir))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(tempSetting.SyncDir);
|
Directory.CreateDirectory(tempSetting.SyncDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
xmlSerializerService.Save("./settings.xml", tempSetting);
|
xmlSerializerService.Save("./settings.xml", tempSetting);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -297,13 +300,27 @@ namespace HeBianGu.App.Disk
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[Displayer(Name = "下载设置", GroupName = "通用设置")]
|
[Displayer(Name = "下载设置", GroupName = "通用设置")]
|
||||||
public class DownloadSetting : LazySettingInstance<DownloadSetting>
|
public class DownloadSetting : LazySettingInstance<DownloadSetting>
|
||||||
{
|
{
|
||||||
|
private int _maxConcurrent;
|
||||||
|
|
||||||
|
[DefaultValue(3)]
|
||||||
|
[Display(Name = "下载并行任务数")]
|
||||||
|
public int MaxConcurrent
|
||||||
|
{
|
||||||
|
get => _maxConcurrent;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_maxConcurrent == value) return;
|
||||||
|
_maxConcurrent = value;
|
||||||
|
RaisePropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private string _downDir;
|
private string _downDir;
|
||||||
|
|
||||||
[Display(Name = "下载目录")]
|
[Display(Name = "下载目录")]
|
||||||
public string DownDir
|
public string DownDir
|
||||||
{
|
{
|
||||||
|
|
@ -315,8 +332,9 @@ namespace HeBianGu.App.Disk
|
||||||
RaisePropertyChanged();
|
RaisePropertyChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool _isSelect;
|
private bool _isSelect;
|
||||||
|
|
||||||
[DefaultValue(false)]
|
[DefaultValue(false)]
|
||||||
[Display(Name = "设置为默认下载目录")]
|
[Display(Name = "设置为默认下载目录")]
|
||||||
public bool IsSelect
|
public bool IsSelect
|
||||||
|
|
@ -330,7 +348,7 @@ namespace HeBianGu.App.Disk
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[Displayer(Name = "同步设置", GroupName = "通用设置")]
|
[Displayer(Name = "同步设置", GroupName = "通用设置")]
|
||||||
public class SyncSetting : LazySettingInstance<SyncSetting>
|
public class SyncSetting : LazySettingInstance<SyncSetting>
|
||||||
|
|
|
||||||
|
|
@ -433,7 +433,7 @@ namespace Hopetry.Services
|
||||||
//var localPath = Path.Combine(filePath, objectKey.Replace('/', Path.DirectorySeparatorChar));
|
//var localPath = Path.Combine(filePath, objectKey.Replace('/', Path.DirectorySeparatorChar));
|
||||||
var localPath = Path.Combine(filePath,
|
var localPath = Path.Combine(filePath,
|
||||||
objectKey[(objectKey.LastIndexOf('/') + 1)..]);
|
objectKey[(objectKey.LastIndexOf('/') + 1)..]);
|
||||||
GetObjectArgs getObjectArgs = new GetObjectArgs()
|
var getObjectArgs = new GetObjectArgs()
|
||||||
.WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName)
|
.WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName)
|
||||||
.WithObject(objectKey)
|
.WithObject(objectKey)
|
||||||
.WithOffsetAndLength(offset, totalBytes - offset)
|
.WithOffsetAndLength(offset, totalBytes - offset)
|
||||||
|
|
@ -477,7 +477,6 @@ namespace Hopetry.Services
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fileStream.Close();
|
fileStream.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ using System.IO;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Data;
|
using System.Windows.Data;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
|
using HeBianGu.App.Disk;
|
||||||
using HeBianGu.Base.WpfBase;
|
using HeBianGu.Base.WpfBase;
|
||||||
using HeBianGu.Service.Mvc;
|
using HeBianGu.Service.Mvc;
|
||||||
using Hopetry.Models;
|
using Hopetry.Models;
|
||||||
|
|
@ -155,7 +156,7 @@ public class DownViewModel : MvcViewModelBase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int _maxConcurrent = 3;
|
private int _maxConcurrent;
|
||||||
|
|
||||||
public int MaxConcurrent
|
public int MaxConcurrent
|
||||||
{
|
{
|
||||||
|
|
@ -164,9 +165,9 @@ public class DownViewModel : MvcViewModelBase
|
||||||
{
|
{
|
||||||
if (value < 1) value = 1;
|
if (value < 1) value = 1;
|
||||||
if (_maxConcurrent == value) return;
|
if (_maxConcurrent == value) return;
|
||||||
|
var delta = value - _maxConcurrent;
|
||||||
_maxConcurrent = value;
|
_maxConcurrent = value;
|
||||||
//AdjustConcurrency();
|
AdjustConcurrency(delta);
|
||||||
RaisePropertyChanged();
|
RaisePropertyChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -204,8 +205,16 @@ public class DownViewModel : MvcViewModelBase
|
||||||
// 加载未完成任务
|
// 加载未完成任务
|
||||||
LoadRunningTasks();
|
LoadRunningTasks();
|
||||||
ClearFinishedCommand = new CustomCommand(DoClearFinishedCommand);
|
ClearFinishedCommand = new CustomCommand(DoClearFinishedCommand);
|
||||||
|
|
||||||
|
Console.WriteLine($"初始下载任务并发数:{DownloadSetting.Instance.MaxConcurrent}");
|
||||||
// 启动任务处理线程
|
// 启动任务处理线程
|
||||||
_semaphore = new SemaphoreSlim(MaxConcurrent);
|
_maxConcurrent = DownloadSetting.Instance.MaxConcurrent;
|
||||||
|
_semaphore = new SemaphoreSlim(DownloadSetting.Instance.MaxConcurrent);
|
||||||
|
DownloadSetting.Instance.PropertyChanged += (sender, args) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine($"当前下载任务并发数:{DownloadSetting.Instance.MaxConcurrent}");
|
||||||
|
MaxConcurrent = DownloadSetting.Instance.MaxConcurrent;
|
||||||
|
};
|
||||||
new Thread(ProcessTasksLoop) { IsBackground = true }.Start();
|
new Thread(ProcessTasksLoop) { IsBackground = true }.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -291,7 +300,7 @@ public class DownViewModel : MvcViewModelBase
|
||||||
public void RefreshHeader()
|
public void RefreshHeader()
|
||||||
{
|
{
|
||||||
RunningTaskHeader = $"下载中({RunningTasks.Count})";
|
RunningTaskHeader = $"下载中({RunningTasks.Count})";
|
||||||
ViewModelLocator.SendViewModel.DownLinkAction.DisplayName = $"下载 {RunningTasks.Count}" ;
|
ViewModelLocator.SendViewModel.DownLinkAction.DisplayName = $"下载 {RunningTasks.Count}";
|
||||||
FinishedTaskHeader = $"已完成({FinishedTasks.Count})";
|
FinishedTaskHeader = $"已完成({FinishedTasks.Count})";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -428,22 +437,43 @@ public class DownViewModel : MvcViewModelBase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AdjustConcurrency()
|
private void AdjustConcurrency(int delta)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("AdjustConcurrency 被调用了");
|
||||||
lock (_semaphore)
|
lock (_semaphore)
|
||||||
{
|
{
|
||||||
// 调整信号量容量
|
// 调整信号量容量
|
||||||
var delta = _maxConcurrent - _semaphore.CurrentCount;
|
|
||||||
if (delta > 0)
|
if (delta > 0)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < delta; i++)
|
_semaphore.Release(delta);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// todo
|
||||||
|
var runTask = _runningTasks.Where(task => task is { Status: "下载中", StopDownTs: not null });
|
||||||
|
var num = -delta;
|
||||||
|
foreach (var item in runTask)
|
||||||
{
|
{
|
||||||
_semaphore.Release();
|
|
||||||
|
_semaphore.WaitAsync();
|
||||||
|
item.StopDownTs.Cancel();
|
||||||
|
item.Status = "等待中";
|
||||||
|
// 速度及剩余时间(视图显示信息)
|
||||||
|
item.Speed = "等待中";
|
||||||
|
_taskQueue.Enqueue(item);
|
||||||
|
if (num-- == 1)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 打开文件夹
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="para"></param>
|
||||||
public void DoOpenDownItemFolder(MinioDownloadTask para)
|
public void DoOpenDownItemFolder(MinioDownloadTask para)
|
||||||
{
|
{
|
||||||
switch (para.Type)
|
switch (para.Type)
|
||||||
|
|
@ -473,6 +503,10 @@ public class DownViewModel : MvcViewModelBase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务取消
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item"></param>
|
||||||
public void DoCancelDownload(MinioDownloadTask item)
|
public void DoCancelDownload(MinioDownloadTask item)
|
||||||
{
|
{
|
||||||
Console.WriteLine("取消下载");
|
Console.WriteLine("取消下载");
|
||||||
|
|
@ -484,6 +518,7 @@ public class DownViewModel : MvcViewModelBase
|
||||||
}
|
}
|
||||||
|
|
||||||
item.Status = "删除中";
|
item.Status = "删除中";
|
||||||
|
item.Speed = "删除中";
|
||||||
using var client = SqlSugarConfig.GetSqlSugarScope();
|
using var client = SqlSugarConfig.GetSqlSugarScope();
|
||||||
client.Deleteable<MinioDownloadTask>().Where(x => x.TaskId == item.TaskId).ExecuteCommand();
|
client.Deleteable<MinioDownloadTask>().Where(x => x.TaskId == item.TaskId).ExecuteCommand();
|
||||||
RunningTasks.Remove(item);
|
RunningTasks.Remove(item);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue