Skip to content

Commit

Permalink
Gui: display both the specified worker number and the background work…
Browse files Browse the repository at this point in the history
…er number, support asynchronously setting the specified worker number
  • Loading branch information
sinsanction committed May 26, 2024
1 parent bfd4820 commit 14ce826
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 75 deletions.
2 changes: 1 addition & 1 deletion OKEGui/OKEGui/Gui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<Button Grid.Column="8" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnMoveTop" Click="BtnMoveTop_Click" Content="置顶" />
<Button Grid.Column="9" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnDelete" Click="BtnDelete_Click" Content="删除" />
<Button Grid.Column="10" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnEmpty" Click="BtnEmpty_Click" Content="清空" />
<TextBlock Margin="0,0,6,0" VerticalAlignment="Center" Grid.Column="11" Grid.Row="0" Name="WorkerNumber" Width="100"></TextBlock>
<TextBlock Margin="0,0,6,0" VerticalAlignment="Center" Grid.Column="11" Grid.Row="0" Name="WorkerNumber" Width="120"></TextBlock>
<Button Grid.Column="12" Grid.Row="0" Margin="0,0,0,0" x:Name="BtnNewWorker" Click="BtnNewWorker_Click" Content="新建工作单元" Width="100" />
</Grid>

Expand Down
41 changes: 25 additions & 16 deletions OKEGui/OKEGui/Gui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ public MainWindow()
for (int i = 0; i < numaCount; i++)
{
WorkerCount++;
wm.AddWorker("工作单元-" + WorkerCount.ToString());
wm.AddWorker(WorkerCount);
}
WorkerNumber.Text = "工作单元数量: " + WorkerCount.ToString();
UpdateWorkerCount();

if (Initializer.Config.memoryTotal == WmiUtils.GetTotalPhysicalMemory())
{
TxtFreeMemory.Text = Initializer.Config.memoryLimit.ToString();
}
}

private void UpdatedActiveRelatedButtons()
private void UpdateActiveRelatedButtons()
{
int activeTaskCount = tm.GetActiveTaskCount();
BtnRun.IsEnabled = activeTaskCount > 0 && !wm.IsRunning;
Expand All @@ -86,16 +86,21 @@ private void UpdatedActiveRelatedButtons()
BtnMoveTop.IsEnabled = activeTaskCount > 0;
}

private void UpdatedCountRelatedButtons()
private void UpdateCountRelatedButtons()
{
int taskCount = tm.GetTaskCount();
BtnDelete.IsEnabled = taskCount > 0;
BtnEmpty.IsEnabled = taskCount > 0;
}

private void UpdateWorkerCount()
{
WorkerNumber.Text = $"工作单元数量: {WorkerCount}({wm.GetBGWorkerCount()})";
}

private void Checkbox_Changed(object sender, RoutedEventArgs e)
{
UpdatedActiveRelatedButtons();
UpdateActiveRelatedButtons();
}

private void BtnRpc_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -133,8 +138,9 @@ private void BtnNew_Click(object sender, RoutedEventArgs e)
{
var wizard = new WizardWindow(wm);
wizard.ShowDialog();
UpdatedActiveRelatedButtons();
UpdatedCountRelatedButtons();
UpdateActiveRelatedButtons();
UpdateCountRelatedButtons();
UpdateWorkerCount();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -189,6 +195,7 @@ private void BtnRun_Click(object sender, RoutedEventArgs e)
BtnRun.IsEnabled = false;
BtnStop.IsEnabled = true;
BtnPause.IsEnabled = true;
UpdateWorkerCount();
}
catch (Exception ex)
{
Expand All @@ -213,12 +220,14 @@ private void BtnStop_Click(object sender, RoutedEventArgs e)
{
wm.Stop();
SubProcessService.KillAll();
UpdateWorkerCount();
}
catch (Exception ex)
{
Logger.Error(ex.StackTrace);
MessageBox.Show("无法终止任务!", "OKEGui", MessageBoxButton.OK, MessageBoxImage.Error);
BtnStop.IsEnabled = true;
UpdateWorkerCount();
return;
}

Expand Down Expand Up @@ -310,8 +319,8 @@ private void BtnDelete_Click(object sender, RoutedEventArgs e)
return;
}

UpdatedActiveRelatedButtons();
UpdatedCountRelatedButtons();
UpdateActiveRelatedButtons();
UpdateCountRelatedButtons();
}

private void BtnEmpty_Click(object sender, RoutedEventArgs e)
Expand All @@ -332,16 +341,16 @@ private void BtnEmpty_Click(object sender, RoutedEventArgs e)
}
}

UpdatedActiveRelatedButtons();
UpdatedCountRelatedButtons();
UpdateActiveRelatedButtons();
UpdateCountRelatedButtons();
}

private void BtnNewWorker_Click(object sender, RoutedEventArgs e)
{
if (wm.AddWorker("工作单元-" + (WorkerCount + 1).ToString()))
if (wm.AddWorker(WorkerCount + 1))
{
WorkerCount++;
WorkerNumber.Text = "工作单元数量: " + WorkerCount.ToString();
UpdateWorkerCount();
}
}

Expand All @@ -352,14 +361,14 @@ private void BtnDeleteWorker_Click(object sender, RoutedEventArgs e)
MessageBox.Show("工作单元删除失败!", "只有一个工作单元了", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (wm.DeleteWorker("工作单元-" + WorkerCount.ToString()))
if (wm.DeleteWorker(WorkerCount.ToString()))
{
WorkerCount--;
WorkerNumber.Text = "工作单元数量: " + WorkerCount.ToString();
UpdateWorkerCount();
}
else
{
MessageBox.Show("工作单元删除失败!", $"工作单元-{WorkerCount}正在运行中", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show("工作单元删除失败!", $"删除工作单元-{WorkerCount}失败,请重试", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

Expand Down
31 changes: 31 additions & 0 deletions OKEGui/OKEGui/Worker/ExecuteTaskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ private void DisableButtonsAfterFinish(MainWindow window)
window.BtnMoveDown.IsEnabled = false;
window.BtnMoveUp.IsEnabled = false;
window.BtnMoveTop.IsEnabled = false;
window.WorkerNumber.Text = $"工作单元数量: {window.WorkerCount}({window.wm.GetBGWorkerCount()})";
}

private void UpdateWorkerCountAfterFinish(MainWindow window)
{
window.WorkerNumber.Text = $"工作单元数量: {window.WorkerCount}({window.wm.GetBGWorkerCount()})";
}

private void WorkerDoWork(object sender, DoWorkEventArgs e)
Expand All @@ -33,6 +39,19 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)

while (IsRunning)
{
// 检查Wid,超过指定工作单元数量的Worker在结束当前任务后直接退出
if (args.Wid > MainWindow.WorkerCount)
{
Logger.Debug($"检测到Wid超过指定工作单元数量,结束当前工作单元{args.Wid}");
lock (o)
{
bgworkerlist.TryRemove(args.Name, out BackgroundWorker v);
Action<MainWindow> updateWorkerAction = new Action<MainWindow>(UpdateWorkerCountAfterFinish);
MainWindow.Dispatcher.BeginInvoke(updateWorkerAction, MainWindow);
}
return;
}

TaskDetail task = args.taskManager.GetNextTask();

// 检查是否已经完成全部任务
Expand All @@ -42,6 +61,9 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
lock (o)
{
bgworkerlist.TryRemove(args.Name, out BackgroundWorker v);
Action<MainWindow> updateWorkerAction = new Action<MainWindow>(UpdateWorkerCountAfterFinish);
MainWindow.Dispatcher.BeginInvoke(updateWorkerAction, MainWindow);

if (bgworkerlist.Count == 0)
{
IsRunning = false;
Expand Down Expand Up @@ -114,6 +136,15 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
}
}

// 检查是否被OKE主线程取消
if (args.bgWorker.CancellationPending)
{
task.Progress = TaskStatus.TaskProgress.ERROR;
task.CurrentStatus = "已终止";
e.Cancel = true;
return;
}

// 执行视频处理工作
if (profile.IsReEncode)
{
Expand Down
Loading

0 comments on commit 14ce826

Please sign in to comment.