mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2024-11-10 00:24:14 +01:00
a hack to unconditionally kill ffmpeg when parent .NET process exits
This commit is contained in:
parent
7938ec3c0a
commit
5ac61011c4
3 changed files with 61 additions and 1 deletions
46
FFMpegCore/Extend/Job.cs
Normal file
46
FFMpegCore/Extend/Job.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using static PInvoke.Kernel32;
|
||||
|
||||
|
||||
namespace FFMpegCore.Extend {
|
||||
public class Job : IDisposable {
|
||||
readonly SafeObjectHandle handle;
|
||||
|
||||
public Job() {
|
||||
this.handle = CreateJobObject(IntPtr.Zero, null);
|
||||
|
||||
var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION {
|
||||
LimitFlags = JOB_OBJECT_LIMIT_FLAGS.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
|
||||
};
|
||||
|
||||
var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = info };
|
||||
|
||||
int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
|
||||
IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length);
|
||||
Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);
|
||||
|
||||
if (!SetInformationJobObject(this.handle, JOBOBJECTINFOCLASS.JobObjectExtendedLimitInformation, extendedInfoPtr, (uint)length))
|
||||
throw new System.ComponentModel.Win32Exception();
|
||||
}
|
||||
|
||||
public bool AddProcess(SafeObjectHandle processHandle) {
|
||||
return AssignProcessToJobObject(this.handle, processHandle);
|
||||
}
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose() {
|
||||
this.Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
|
||||
private void Dispose(bool disposing) => this.Close();
|
||||
|
||||
public void Close() => this.handle.Close();
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -9,6 +9,12 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FFMpegCore.Enums;
|
||||
using FFMpegCore.Exceptions;
|
||||
using FFMpegCore.Extend;
|
||||
using FFMpegCore.Helpers;
|
||||
using Instances;
|
||||
|
||||
using PInvoke;
|
||||
|
||||
namespace FFMpegCore
|
||||
{
|
||||
|
@ -141,7 +147,14 @@ private async Task<IProcessResult> Process(ProcessArguments processArguments, Ca
|
|||
|
||||
_ffMpegArguments.Pre();
|
||||
|
||||
using var instance = processArguments.Start();
|
||||
using var instance = (ProcessInstance)processArguments.Start();
|
||||
|
||||
using var job = new Job();
|
||||
var processHack = (Process)typeof(ProcessInstance).GetField("_process", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
||||
.GetValue(instance);
|
||||
using var instanceHandle = new Kernel32.SafeObjectHandle(processHack.Handle, ownsHandle: false);
|
||||
job.AddProcess(instanceHandle);
|
||||
|
||||
var cancelled = false;
|
||||
void OnCancelEvent(object sender, int timeout)
|
||||
{
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Instances" Version="3.0.0"/>
|
||||
<PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
|
||||
<PackageReference Include="System.Text.Json" Version="7.0.1"/>
|
||||
</ItemGroup>
|
||||
|
||||
|
|
Loading…
Reference in a new issue