Skip to content

Commit

Permalink
Added pausing/resuming for interpolation, async (background) web checks
Browse files Browse the repository at this point in the history
  • Loading branch information
n00mkrad committed Apr 29, 2021
1 parent abc9817 commit d64e81c
Show file tree
Hide file tree
Showing 17 changed files with 332 additions and 46 deletions.
File renamed without changes.
61 changes: 61 additions & 0 deletions Code/Extensions/ProcessExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Win32Interop;

namespace Flowframes.Extensions
{
public static class ProcessExtensions
{
[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);

public static void Suspend(this Process process)
{
foreach (ProcessThread thread in process.Threads)
{
var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);

if (pOpenThread == IntPtr.Zero)
break;

SuspendThread(pOpenThread);
}
}

public static void Resume(this Process process)
{
foreach (ProcessThread thread in process.Threads)
{
var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);

if (pOpenThread == IntPtr.Zero)
break;

ResumeThread(pOpenThread);
}
}
}
}
7 changes: 6 additions & 1 deletion Code/Flowframes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@
<Compile Include="Data\PseudoUniqueFile.cs" />
<Compile Include="Data\ResumeState.cs" />
<Compile Include="Data\SubtitleTrack.cs" />
<Compile Include="Extensions\ProcessExtensions.cs" />
<Compile Include="Forms\BatchForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -377,7 +378,7 @@
<Compile Include="MiscUtils\Benchmarker.cs" />
<Compile Include="MiscUtils\FrameRename.cs" />
<Compile Include="OS\AiProcess.cs" />
<Compile Include="ExtensionMethods.cs" />
<Compile Include="Extensions\ExtensionMethods.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
Expand All @@ -392,6 +393,7 @@
<Compile Include="IO\Logger.cs" />
<Compile Include="Magick\Converter.cs" />
<Compile Include="Magick\Dedupe.cs" />
<Compile Include="OS\AiProcessSuspend.cs" />
<Compile Include="OS\NvApi.cs" />
<Compile Include="OS\OSUtils.cs" />
<Compile Include="OS\Python.cs" />
Expand Down Expand Up @@ -461,6 +463,9 @@
</ItemGroup>
<ItemGroup>
<Content Include="FlowframesLogo2021.ico" />
<None Include="Resources\baseline_play_arrow_white_48dp.png" />
<None Include="Resources\baseline_pause_white_48dp.png" />
<None Include="Resources\baseline_stop_white_48dp.png" />
<None Include="Resources\devmode.bat" />
<None Include="Resources\flowframesIcoNew-512px.png" />
<None Include="Resources\flowframesIcoNew.png" />
Expand Down
87 changes: 69 additions & 18 deletions Code/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 26 additions & 10 deletions Code/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ async Task Checks()
{
try
{
await Task.Delay(100);
await StartupChecks.SymlinksCheck();
await Updater.UpdateModelList(); // Update AI model list
await Updater.AsyncUpdateCheck(); // Check for Flowframes updates
await GetWebInfo.LoadNews(newsLabel); // Loads news/MOTD
await GetWebInfo.LoadPatronListCsv(patronsLabel); // Load patron list
//await Task.Delay(100);
Task.Run(() => Updater.UpdateModelList());
Task.Run(() => Updater.AsyncUpdateCheck());
Task.Run(() => GetWebInfo.LoadNews(newsLabel));
Task.Run(() => GetWebInfo.LoadPatronListCsv(patronsLabel));
await Python.CheckCompression();
await StartupChecks.SymlinksCheck();

}
catch (Exception e)
{
Expand Down Expand Up @@ -106,6 +107,7 @@ void HandleArguments()

public HTTabControl GetMainTabControl() { return mainTabControl; }
public TextBox GetInputFpsTextbox () { return fpsInTbox; }
public Button GetPauseBtn() { return pauseBtn; }

public bool IsInFocus() { return (ActiveForm == this); }

Expand Down Expand Up @@ -223,6 +225,7 @@ public void runBtn_Click(object sender, EventArgs e)
if (!BatchProcessing.busy) // Don't load values from gui if batch processing is used
Interpolate.current = GetCurrentSettings();

AiProcessSuspend.Reset();
Interpolate.Start();
}

Expand Down Expand Up @@ -324,12 +327,15 @@ public void SetWorking(bool state, bool allowCancel = true)
Control[] controlsToDisable = new Control[] { runBtn, runStepBtn, stepSelector, settingsBtn };
Control[] controlsToHide = new Control[] { runBtn, runStepBtn, stepSelector };
progressCircle.Visible = state;
cancelBtn.Visible = state;
busyControlsPanel.Visible = state;

foreach (Control c in controlsToDisable)
c.Enabled = !state;

foreach (Control c in controlsToHide)
c.Visible = !state;
cancelBtn.Enabled = allowCancel;

busyControlsPanel.Enabled = allowCancel;
Program.busy = state;
Program.mainForm.UpdateStepByStepControls();
}
Expand Down Expand Up @@ -399,8 +405,13 @@ public void DragDropHandler(string[] files)

private void cancelBtn_Click(object sender, EventArgs e)
{
SetTab("interpolation");
Interpolate.Cancel();
DialogResult dialog = MessageBox.Show($"Are you sure you want to cancel the interpolation?", "Are you sure?", MessageBoxButtons.YesNo);

if (dialog == DialogResult.Yes)
{
SetTab("interpolation");
Interpolate.Cancel();
}
}

private void discordBtn_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -550,5 +561,10 @@ private void scnDetectTestBtn_Click(object sender, EventArgs e)
{
Magick.SceneDetect.RunSceneDetection(inputTbox.Text.Trim());
}

private void pauseBtn_Click(object sender, EventArgs e)
{
AiProcessSuspend.SuspendResumeAi(!AiProcessSuspend.aiProcFrozen);
}
}
}
2 changes: 1 addition & 1 deletion Code/IO/IOUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ public static void OverwriteFileWithText (string path, string text = "THIS IS A
}
catch (Exception e)
{
Logger.Log($"OverwriteWithText failed for '{path}': {e.Message}");
Logger.Log($"OverwriteWithText failed for '{path}': {e.Message}", true);
}
}

Expand Down
Loading

0 comments on commit d64e81c

Please sign in to comment.