Compare commits
2 Commits
326ff82669
...
8125ccaae7
| Author | SHA1 | Date |
|---|---|---|
|
|
8125ccaae7 | |
|
|
32f437432b |
|
|
@ -4,24 +4,32 @@
|
|||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:h="https://github.com/HeBianGu"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:disk="clr-namespace:HeBianGu.App.Disk"
|
||||
xmlns:system="clr-namespace:System;assembly=HeBianGu.Base.WpfBase"
|
||||
xmlns:local="clr-namespace:Hopetry.ViewModel.Send"
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800"
|
||||
mc:Ignorable="d">
|
||||
<UserControl.Resources>
|
||||
</UserControl.Resources>
|
||||
<Grid DataContext="{StaticResource S.ViewModelLocator.Locator}">
|
||||
|
||||
<DockPanel Margin="0,0,10,0">
|
||||
<h:Row DockPanel.Dock="Top" Margin="0 ,8,0,8" Style="{DynamicResource {x:Static h:Row.Column10Key}}">
|
||||
|
||||
<Button Grid.Column="0" Content="全部移除"
|
||||
Margin="10,0"
|
||||
Visibility="{Binding DownViewModel.Tab1Visibility}"
|
||||
Style="{DynamicResource {x:Static h:ButtonKeys.Accent}}" />
|
||||
<Button Grid.Column="0"
|
||||
Margin="10,0"
|
||||
Visibility="{Binding DownViewModel.Tab0Visibility}"
|
||||
Content="全部暂停"
|
||||
Command="{Binding DownViewModel.AddTaskCommand}"
|
||||
Style="{DynamicResource {x:Static h:ButtonKeys.BorderBrushTransparent}}" />
|
||||
Style="{DynamicResource {x:Static h:ButtonKeys.Accent}}" />
|
||||
<Button Grid.Column="1" Content="全部取消"
|
||||
Style="{DynamicResource {x:Static h:ButtonKeys.BorderBrushTransparent}}" />
|
||||
Margin="10,0"
|
||||
Visibility="{Binding DownViewModel.Tab0Visibility}"
|
||||
Style="{DynamicResource {x:Static h:ButtonKeys.Accent}}" />
|
||||
</h:Row>
|
||||
<TabControl>
|
||||
<TabControl SelectedIndex="{Binding DownViewModel.TabIndex, Mode=TwoWay}">
|
||||
<TabItem Header="{Binding DownViewModel.RunningTaskHeader}">
|
||||
<ListBox h:Cattach.ItemHeight="Auto"
|
||||
ItemsSource="{Binding DownViewModel.RunningTasksView}"
|
||||
|
|
@ -64,26 +72,25 @@
|
|||
Grid.Column="2"
|
||||
HorizontalAlignment="Right"
|
||||
Style="{DynamicResource {x:Static h:TextBlockKeys.Default}}"
|
||||
Text="10:10:10" />
|
||||
Text="时间未知" />
|
||||
|
||||
<TextBlock Grid.Row="0"
|
||||
Grid.Column="3"
|
||||
Style="{DynamicResource {x:Static h:TextBlockKeys.Default}}"
|
||||
Text="{Binding FileName}" />
|
||||
Text="" />
|
||||
|
||||
<h:FProgressBar Grid.Column="3"
|
||||
Height="15"
|
||||
CornerRadius="2"
|
||||
Maximum="100"
|
||||
Value="11">
|
||||
</h:FProgressBar>
|
||||
Value="11" />
|
||||
|
||||
<TextBlock Grid.Row="1"
|
||||
Grid.Column="3"
|
||||
Margin="-3,0"
|
||||
HorizontalAlignment="Left"
|
||||
Style="{DynamicResource {x:Static h:TextBlockKeys.Default}}"
|
||||
Text="{Binding FileName}" />
|
||||
Text="N/A kb/s" />
|
||||
|
||||
<!--操作-->
|
||||
<StackPanel Grid.RowSpan="2"
|
||||
|
|
@ -162,8 +169,7 @@
|
|||
Height="15"
|
||||
CornerRadius="2"
|
||||
Maximum="100"
|
||||
Value="11">
|
||||
</h:FProgressBar>
|
||||
Value="11" />
|
||||
|
||||
<TextBlock Grid.Row="1"
|
||||
Grid.Column="3"
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
using HeBianGu.Base.WpfBase;
|
||||
|
|
@ -25,7 +26,51 @@ public class DownViewModel : MvcViewModelBase
|
|||
private CancellationTokenSource _processingCts = new();
|
||||
public int count { get; set; }
|
||||
private readonly ReaderWriterLockSlim _lock = new();
|
||||
|
||||
private Visibility _tab0Visibility;
|
||||
|
||||
public Visibility Tab0Visibility
|
||||
{
|
||||
get => _tab0Visibility;
|
||||
set
|
||||
{
|
||||
_tab0Visibility = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private Visibility _tab1Visibility;
|
||||
public Visibility Tab1Visibility
|
||||
{
|
||||
get => _tab1Visibility;
|
||||
set
|
||||
{
|
||||
_tab1Visibility = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
private int _tabIndex;
|
||||
|
||||
public int TabIndex
|
||||
{
|
||||
get => _tabIndex;
|
||||
set
|
||||
{
|
||||
_tabIndex = value;
|
||||
if (_tabIndex == 0)
|
||||
{
|
||||
Tab0Visibility = Visibility.Visible;
|
||||
Tab1Visibility = Visibility.Hidden;
|
||||
}
|
||||
else if (_tabIndex == 1)
|
||||
{
|
||||
Tab0Visibility = Visibility.Hidden;
|
||||
Tab1Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 全部任务动态标题
|
||||
|
|
@ -135,28 +180,6 @@ public class DownViewModel : MvcViewModelBase
|
|||
e.Accepted = b;
|
||||
};
|
||||
FinishedTasksView = cvsFinished.View;
|
||||
RefreshViewHeader();
|
||||
|
||||
// 关键:订阅数据源变更事件,强制视图刷新
|
||||
AllTasks.CollectionChanged += (s, e) =>
|
||||
{
|
||||
RunningTasksView.Refresh();
|
||||
FinishedTasksView.Refresh();
|
||||
};
|
||||
|
||||
ClearFinishedCommand = new CustomCommand(() =>
|
||||
{
|
||||
// todo 删除已完成的下载记录
|
||||
});
|
||||
|
||||
// 启动任务处理线程
|
||||
_semaphore = new SemaphoreSlim(_maxConcurrent);
|
||||
new Thread(ProcessTasksLoop) { IsBackground = true }.Start();
|
||||
//AddTaskCommand = new ActionCommand(AddTask);
|
||||
}
|
||||
|
||||
private void RefreshViewHeader()
|
||||
{
|
||||
if (FinishedTasksView is ListCollectionView finishListView)
|
||||
{
|
||||
FinishedTaskHeader = $"已完成({finishListView.Count})";
|
||||
|
|
@ -166,6 +189,31 @@ public class DownViewModel : MvcViewModelBase
|
|||
{
|
||||
RunningTaskHeader = $"下载中({runningListView.Count})";
|
||||
}
|
||||
|
||||
// 关键:订阅数据源变更事件,强制视图刷新
|
||||
AllTasks.CollectionChanged += (s, e) =>
|
||||
{
|
||||
RunningTasksView.Refresh();
|
||||
FinishedTasksView.Refresh();
|
||||
if (FinishedTasksView is ListCollectionView finishListView)
|
||||
{
|
||||
FinishedTaskHeader = $"已完成({finishListView.Count})";
|
||||
}
|
||||
|
||||
if (RunningTasksView is ListCollectionView runningListView)
|
||||
{
|
||||
RunningTaskHeader = $"下载中({runningListView.Count})";
|
||||
}
|
||||
};
|
||||
|
||||
ClearFinishedCommand = new CustomCommand(() =>
|
||||
{
|
||||
// todo 删除已完成的下载记录
|
||||
});
|
||||
// 启动任务处理线程
|
||||
_semaphore = new SemaphoreSlim(_maxConcurrent);
|
||||
new Thread(ProcessTasksLoop) { IsBackground = true }.Start();
|
||||
//AddTaskCommand = new ActionCommand(AddTask);
|
||||
}
|
||||
|
||||
private async void ProcessTasksLoop()
|
||||
|
|
@ -173,29 +221,29 @@ public class DownViewModel : MvcViewModelBase
|
|||
int c1 = 1;
|
||||
while (!_processingCts.IsCancellationRequested)
|
||||
{
|
||||
await _semaphore.WaitAsync();
|
||||
Console.WriteLine("获得下载许可");
|
||||
if (_taskQueue.TryDequeue(out var task))
|
||||
{
|
||||
Console.WriteLine($"开启下载任务:{c1++}");
|
||||
Console.WriteLine("存在可下载任务,正在申请许可...");
|
||||
await _semaphore.WaitAsync();
|
||||
Console.WriteLine("申请下载许可成功!马上开启下载");
|
||||
// 启动新线程执行下载任务
|
||||
// _ = Task.Run(() => ExecuteTaskAsync(task));
|
||||
_ = ExecuteTaskAsync(task).ContinueWith(_ => _semaphore.Release());
|
||||
}
|
||||
|
||||
Thread.Sleep(100);
|
||||
// 无任务时,进入低功耗轮询
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddTask(string bucketName, string objectKey)
|
||||
{
|
||||
var downDir = ViewModelLocator.SyncViewModel.SyncDir;
|
||||
var task = new MinioDownloadTask(_minioService, bucketName, objectKey,downDir);
|
||||
var task = new MinioDownloadTask(_minioService, bucketName, objectKey, downDir);
|
||||
using var client = SqlSugarConfig.GetSqlSugarScope();
|
||||
client.Insertable(task).ExecuteCommandIdentityIntoEntity();
|
||||
AllTasks.Add(task);
|
||||
_taskQueue.Enqueue(task);
|
||||
RefreshViewHeader();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -204,12 +252,13 @@ public class DownViewModel : MvcViewModelBase
|
|||
try
|
||||
{
|
||||
await task.StartDownload();
|
||||
var x = AllTasks.IndexOf(task);
|
||||
var temp = AllTasks[x];
|
||||
temp.Status = "已完成";
|
||||
Console.WriteLine($"所有任务取出任务id {temp.TaskId} 完成任务id:{task.TaskId}" );
|
||||
var x = AllTasks.IndexOf(task);
|
||||
AllTasks[AllTasks.IndexOf(task)] = task;
|
||||
/*var temp = AllTasks[x];
|
||||
temp.Status = "已完成";*/
|
||||
//Console.WriteLine($"所有任务取出任务id {temp.TaskId} 完成任务id:{task.TaskId}" );
|
||||
Console.WriteLine($"完成任务在全部任务中的位置 :{x}");
|
||||
RefreshViewHeader();
|
||||
//RefreshViewHeader();
|
||||
Console.WriteLine($"异步下载完成:{task.FileName}");
|
||||
}
|
||||
finally
|
||||
|
|
@ -258,4 +307,6 @@ public class DownViewModel : MvcViewModelBase
|
|||
protected override void Loaded(string args)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue