Skip to content

Commit

Permalink
Added pause and resume functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
LittlePox committed Nov 14, 2019
1 parent e2a5cde commit 650448d
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 20 deletions.
18 changes: 10 additions & 8 deletions OKEGui/OKEGui/Gui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,21 @@
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>

<Button Height="23" Grid.Column="0" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnNew" VerticalAlignment="Top" Click="BtnNew_Click" Content="新建任务" />
<Button Height="23" Grid.Column="1" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnRun" VerticalAlignment="Top" Click="BtnRun_Click" Content="运行" />
<Button Height="23" Grid.Column="2" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnStop" VerticalAlignment="Top" Click="BtnStop_Click" Content="停止" />
<Button Height="23" Grid.Column="3" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnChap" VerticalAlignment="Top" Click="BtnChap_Click" Content="检查章节" />
<Button Height="23" Grid.Column="4" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnMoveup" VerticalAlignment="Top" Click="BtnMoveup_Click" Content="上移" />
<Button Height="23" Grid.Column="5" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnMoveDown" VerticalAlignment="Top" Click="BtnMoveDown_Click" Content="下移" />
<Button Height="23" Grid.Column="6" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnDelete" VerticalAlignment="Top" Click="BtnDelete_Click" Content="删除" />
<Button Height="23" Grid.Column="7" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnEmpty" VerticalAlignment="Top" Click="BtnEmpty_Click" Content="清空" />
<TextBlock Height="23" Margin="0,4,6,0" Grid.Column="8" Grid.Row="0" Name="WorkerNumber"></TextBlock>
<Button Height="23" Grid.Column="9" Grid.Row="0" Margin="0,0,0,0" x:Name="BtnNewWorker" VerticalAlignment="Top" Click="BtnNewWorker_Click" Content="新建工作单元" Width="100" />
<Button Height="23" Grid.Column="2" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnPause" VerticalAlignment="Top" Click="BtnPause_Click" Content="暂停" />
<Button Height="23" Grid.Column="3" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnResume" VerticalAlignment="Top" Click="BtnResume_Click" Content="恢复" />
<Button Height="23" Grid.Column="4" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnChap" VerticalAlignment="Top" Click="BtnChap_Click" Content="检查章节" />
<Button Height="23" Grid.Column="5" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnMoveup" VerticalAlignment="Top" Click="BtnMoveup_Click" Content="上移" />
<Button Height="23" Grid.Column="6" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnMoveDown" VerticalAlignment="Top" Click="BtnMoveDown_Click" Content="下移" />
<Button Height="23" Grid.Column="7" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnDelete" VerticalAlignment="Top" Click="BtnDelete_Click" Content="删除" />
<Button Height="23" Grid.Column="8" Grid.Row="0" Margin="0,0,6,0" x:Name="BtnEmpty" VerticalAlignment="Top" Click="BtnEmpty_Click" Content="清空" />
<TextBlock Height="23" Margin="0,4,6,0" Grid.Column="9" Grid.Row="0" Name="WorkerNumber"></TextBlock>
<Button Height="23" Grid.Column="10" Grid.Row="0" Margin="0,0,0,0" x:Name="BtnNewWorker" VerticalAlignment="Top" Click="BtnNewWorker_Click" Content="新建工作单元" Width="100" />
</Grid>

<Grid Margin="20, 0, 10, 10" VerticalAlignment="Bottom">
Expand Down
26 changes: 20 additions & 6 deletions OKEGui/OKEGui/Gui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using OKEGui.Task;
using OKEGui.Worker;

namespace OKEGui
Expand Down Expand Up @@ -34,12 +35,13 @@ public MainWindow()

listView1.ItemsSource = tm.taskStatus;

wm = new WorkerManager(tm);
wm = new WorkerManager(this, tm);

BtnRun.IsEnabled = false;
BtnMoveDown.IsEnabled = false;
BtnMoveup.IsEnabled = false;
BtnStop.IsEnabled = false;
BtnPause.IsEnabled = false;
BtnResume.IsEnabled = false;
BtnChap.IsEnabled = false;

// 初始的worker数量等于Numa数量。
Expand All @@ -54,8 +56,11 @@ public MainWindow()

private void Checkbox_Changed(object sender, RoutedEventArgs e)
{
BtnRun.IsEnabled = tm.HasNextTask();
BtnChap.IsEnabled = BtnRun.IsEnabled;
if (!wm.IsRunning)
{
BtnRun.IsEnabled = tm.HasNextTask();
BtnChap.IsEnabled = BtnRun.IsEnabled;
}
}

private void BtnNew_Click(object sender, RoutedEventArgs e)
Expand All @@ -77,9 +82,17 @@ private void BtnNew_Click(object sender, RoutedEventArgs e)

}

private void BtnStop_Click(object sender, RoutedEventArgs e)
private void BtnPause_Click(object sender, RoutedEventArgs e)
{
// TODO
BtnPause.IsEnabled = false;
PauseResumeService.PauseAll();
BtnResume.IsEnabled = true;
}
private void BtnResume_Click(object sender, RoutedEventArgs e)
{
BtnResume.IsEnabled = false;
PauseResumeService.ResumeAll();
BtnPause.IsEnabled = true;
}

private void BtnRun_Click(object sender, RoutedEventArgs e)
Expand All @@ -99,6 +112,7 @@ private void BtnRun_Click(object sender, RoutedEventArgs e)
BtnDeleteWorker.IsEnabled = false;
BtnEmpty.IsEnabled = false;
BtnRun.IsEnabled = false;
BtnPause.IsEnabled = true;
}
catch (Exception ex)
{
Expand Down
2 changes: 2 additions & 0 deletions OKEGui/OKEGui/OKEGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Management" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Transactions" />
Expand Down Expand Up @@ -106,6 +107,7 @@
<Compile Include="Task\AddTaskService.cs" />
<Compile Include="Task\ChapterService.cs" />
<Compile Include="Task\EpisodeProfile.cs" />
<Compile Include="Task\PauseResumeService.cs" />
<Compile Include="Task\TaskProfile.cs" />
<Compile Include="Model\Track\AudioTrack.cs" />
<Compile Include="Model\Track\ChapterTrack.cs" />
Expand Down
2 changes: 1 addition & 1 deletion OKEGui/OKEGui/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.7.*")]
[assembly: AssemblyVersion("5.8.*")]
119 changes: 119 additions & 0 deletions OKEGui/OKEGui/Task/PauseResumeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace OKEGui.Task
{
public class PauseResumeService
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

[Flags]
public enum ThreadAccess : int
{
TERMINATE = (0x0001),
SUSPEND_RESUME = (0x0002),
GET_CONTEXT = (0x0008),
SET_CONTEXT = (0x0010),
SET_INFORMATION = (0x0020),
QUERY_INFORMATION = (0x0040),
SET_THREAD_TOKEN = (0x0080),
IMPERSONATE = (0x0100),
DIRECT_IMPERSONATION = (0x0200)
}

[DllImport("kernel32.dll")]
static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("kernel32.dll")]
static extern uint SuspendThread(IntPtr hThread);
[DllImport("kernel32.dll")]
static extern int ResumeThread(IntPtr hThread);
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);


private static void SuspendProcess(Process process)
{
foreach (ProcessThread pT in process.Threads)
{
IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);

if (pOpenThread == IntPtr.Zero)
{
continue;
}

SuspendThread(pOpenThread);
CloseHandle(pOpenThread);
}
}

private static void ResumeProcess(Process process)
{
foreach (ProcessThread pT in process.Threads)
{
IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);

if (pOpenThread == IntPtr.Zero)
{
continue;
}

int suspendCount = 2;
while (suspendCount > 1)
{
suspendCount = ResumeThread(pOpenThread);
}

CloseHandle(pOpenThread);
}
}

private static List<Process> getChildProcesses(Process process)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"SELECT *" +
" FROM Win32_Process" +
" WHERE ParentProcessId=" + process.Id.ToString());
ManagementObjectCollection collection = searcher.Get();
List<Process> res = new List<Process>();
if (collection.Count > 0)
{
foreach (ManagementBaseObject item in collection)
{
UInt32 childProcessId = (UInt32)item["ProcessId"];
if ((int)childProcessId != Process.GetCurrentProcess().Id)
{
Process childProcess = Process.GetProcessById((int)childProcessId);
res.Add(childProcess);
res.AddRange(getChildProcesses(childProcess));
}
}
}
return res;
}

public static void PauseAll()
{
List<Process> allProcesses = getChildProcesses(Process.GetCurrentProcess());
foreach (Process i in allProcesses)
{
SuspendProcess(i);
}
}

public static void ResumeAll()
{
List<Process> allProcesses = getChildProcesses(Process.GetCurrentProcess());
foreach (Process i in allProcesses)
{
ResumeProcess(i);
}
}
}
}
2 changes: 1 addition & 1 deletion OKEGui/OKEGui/Utils/Cleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public List<string> Rename(string inputFile, List<string> whiteList)
File.Move(oldFile, newFile);
files[i] = newFile;
}
catch (Exception e)
catch (Exception)
{
Logger.Error($"无法备份{oldFile},直接删除。");
File.Delete(oldFile);
Expand Down
13 changes: 10 additions & 3 deletions OKEGui/OKEGui/Worker/ExecuteTaskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public partial class WorkerManager
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

private void DisableButtonsAfterFinish(MainWindow window)
{
window.BtnPause.IsEnabled = false;
window.BtnResume.IsEnabled = false;
}

private void WorkerDoWork(object sender, DoWorkEventArgs e)
{
WorkerArgs args = (WorkerArgs)e.Argument;
Expand All @@ -26,15 +32,16 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
// 检查是否已经完成全部任务
if (task == null)
{
// 全部工作完成
Logger.Debug("所有任务已经完成");
Action<MainWindow> disableButtonsAction = new Action<MainWindow>(DisableButtonsAfterFinish);
MainWindow.Dispatcher.BeginInvoke(disableButtonsAction, MainWindow);
lock (o)
{
bgworkerlist.TryRemove(args.Name, out BackgroundWorker v);

if (bgworkerlist.Count == 0)
{
IsRunning = false;
Debugger.Log(0, "", "Ready to call the after finish process\n");
Logger.Info("Ready to call the after finish process");
AfterFinish?.Invoke();
}
}
Expand Down
4 changes: 3 additions & 1 deletion OKEGui/OKEGui/Worker/WorkerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public struct WorkerArgs
// Worker执行Task的具体实现,见ExecuteTaskService里的WorkerDoWork()
public partial class WorkerManager
{
public MainWindow MainWindow;
public TaskManager tm;

private List<string> workerList;
Expand All @@ -40,11 +41,12 @@ public partial class WorkerManager

public Callback AfterFinish = null;

public WorkerManager(TaskManager taskManager)
public WorkerManager(MainWindow mainWindow, TaskManager taskManager)
{
workerList = new List<string>();
bgworkerlist = new ConcurrentDictionary<string, BackgroundWorker>();
workerType = new ConcurrentDictionary<string, WorkerType>();
MainWindow = mainWindow;
tm = taskManager;
IsRunning = false;
tempCounter = 0;
Expand Down

0 comments on commit 650448d

Please sign in to comment.