Skip to content

Commit

Permalink
Add Pause/Resume
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkl58 committed Apr 6, 2021
1 parent c586635 commit 03f8234
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 7 deletions.
12 changes: 8 additions & 4 deletions NotEnoughAV1Encodes/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1107,10 +1107,9 @@
</Button>
<ProgressBar x:Name="ProgressBar" Margin="21,0,21,19" ValueChanged="ProgressBar_ValueChanged" Height="34" VerticalAlignment="Bottom"/>
<Label x:Name="LabelProgressBar" Content="" Margin="21,0,21,19" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Height="34" VerticalAlignment="Bottom"/>
<Button x:Name="ButtonStopEncode" Margin="447,20,0,0" VerticalAlignment="Top" Height="48" HorizontalAlignment="Left" Width="135" Background="{x:Null}" BorderBrush="#FF707070" Click="ButtonStopEncode_Click">
<Canvas HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="100">
<Image HorizontalAlignment="Left" Height="47" VerticalAlignment="Top" Width="47" Source="img/stop.png" Canvas.Top="-6" Canvas.Left="-9"/>
<Label Content="Cancel" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="0.463,0.491" Canvas.Left="38" Height="30" Canvas.Top="2" FontSize="13" />
<Button x:Name="ButtonStopEncode" Margin="514,20,0,0" VerticalAlignment="Top" Height="48" HorizontalAlignment="Left" Width="68" Background="{x:Null}" BorderBrush="#FF707070" Click="ButtonStopEncode_Click">
<Canvas HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="62">
<Image HorizontalAlignment="Left" Height="47" VerticalAlignment="Top" Width="48" Source="img/stop.png" Canvas.Top="-7" Canvas.Left="3"/>
</Canvas>
</Button>
<Label Content="Preset:" HorizontalAlignment="Left" Margin="21,78,0,0" VerticalAlignment="Top" Height="29"/>
Expand All @@ -1119,6 +1118,11 @@
<Button x:Name="ButtonSetDefaultPreset" Content="Set Default" HorizontalAlignment="Left" Margin="317,78,0,0" VerticalAlignment="Top" Width="76" Height="29" Background="{x:Null}" Click="ButtonSetDefaultPreset_Click"/>
<Button x:Name="ButtonDeletePreset" Content="Delete" HorizontalAlignment="Left" Margin="392,78,0,0" VerticalAlignment="Top" Width="50" Height="29" Background="{x:Null}" Click="ButtonDeletePreset_Click" />
<Button x:Name="ButtonExpandCollapseWindow" Content="Resize Window" HorizontalAlignment="Left" Margin="447,77,0,0" VerticalAlignment="Top" Width="135" Height="29" Background="{x:Null}" Click="ButtonExpandCollapseWindow_Click"/>
<Button x:Name="ButtonPauseEncode" Margin="447,20,0,0" VerticalAlignment="Top" Height="48" HorizontalAlignment="Left" Width="68" Background="{x:Null}" BorderBrush="#FF707070" Click="ButtonPauseEncode_Click">
<Canvas HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="62">
<Image x:Name="ImagePauseResume" HorizontalAlignment="Left" Height="47" VerticalAlignment="Top" Width="48" Source="img/pause.png" Canvas.Top="-7" Canvas.Left="3"/>
</Canvas>
</Button>

</Grid>
</mah:MetroWindow>
34 changes: 34 additions & 0 deletions NotEnoughAV1Encodes/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,40 @@ private void ButtonSetTempPath_Click(object sender, RoutedEventArgs e)
SaveSettingsTab();
}
}
public static bool encode_paused = false;
private void ButtonPauseEncode_Click(object sender, RoutedEventArgs e)
{
if (EncodeStarted == true)
{
if (encode_paused)
{
var process = Process.GetProcessesByName("ffmpeg");
foreach (var p in process)
{
suspend.ResumeProcess(p.Id);
}
BitmapImage image = new BitmapImage(new Uri("/NotEnoughAV1Encodes;component/img/pause.png", UriKind.Relative));
ImagePauseResume.Source = image;
encode_paused = false;
}
else
{
var process = Process.GetProcessesByName("ffmpeg");
foreach (var p in process)
{
suspend.SuspendProcess(p.Id);
}
BitmapImage image = new BitmapImage(new Uri("/NotEnoughAV1Encodes;component/img/resume.png", UriKind.Relative));
ImagePauseResume.Source = image;
encode_paused = true;
}
}
else
{
SmallFunctions.PlayStopSound();
MessageBox.Show("Encode has not started yet!", "Attention", MessageBoxButton.OK, MessageBoxImage.Information);
}
}

private void ButtonStartEncode_Click(object sender, RoutedEventArgs e)
{
Expand Down
9 changes: 8 additions & 1 deletion NotEnoughAV1Encodes/NotEnoughAV1Encodes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
Expand Down Expand Up @@ -110,6 +110,7 @@
<DependentUpon>SavePreset.xaml</DependentUpon>
</Compile>
<Compile Include="SmallFunctions.cs" />
<Compile Include="suspend.cs" />
<Compile Include="Updater.xaml.cs">
<DependentUpon>Updater.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -224,5 +225,11 @@
<ItemGroup>
<Resource Include="img\video_icon.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="img\pause.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="img\resume.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
4 changes: 2 additions & 2 deletions NotEnoughAV1Encodes/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.7.1.0")]
[assembly: AssemblyFileVersion("1.7.1.0")]
[assembly: AssemblyVersion("1.8.0.0")]
[assembly: AssemblyFileVersion("1.8.0.0")]
[assembly: NeutralResourcesLanguage("en")]
Binary file added NotEnoughAV1Encodes/img/pause.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added NotEnoughAV1Encodes/img/resume.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions NotEnoughAV1Encodes/suspend.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace NotEnoughAV1Encodes
{
class suspend
{
[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);


public static void SuspendProcess(int pid)
{
var process = Process.GetProcessById(pid); // throws exception if process does not exist

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);
}
}

public static void ResumeProcess(int pid)
{
var process = Process.GetProcessById(pid);

if (process.ProcessName == string.Empty)
return;

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

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

var suspendCount = 0;
do
{
suspendCount = ResumeThread(pOpenThread);
} while (suspendCount > 0);

CloseHandle(pOpenThread);
}
}
}
}

0 comments on commit 03f8234

Please sign in to comment.