diff --git a/Examples/NestedJobs/App.config b/Examples/NestedJobs/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/Examples/NestedJobs/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Examples/NestedJobs/NestedJobs.csproj b/Examples/NestedJobs/NestedJobs.csproj new file mode 100644 index 0000000..389728c --- /dev/null +++ b/Examples/NestedJobs/NestedJobs.csproj @@ -0,0 +1,59 @@ + + + + + Debug + AnyCPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9} + Exe + NestedJobs + NestedJobs + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + {0e50e067-74e6-41e9-8e69-b485813ca7d8} + JobManagement + + + + \ No newline at end of file diff --git a/Examples/NestedJobs/Program.cs b/Examples/NestedJobs/Program.cs new file mode 100644 index 0000000..a40f936 --- /dev/null +++ b/Examples/NestedJobs/Program.cs @@ -0,0 +1,81 @@ +using JobManagement; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace NestedJobs +{ + class Program + { + static readonly System.Threading.ManualResetEvent FinishEvent = new System.Threading.ManualResetEvent(false); + static bool nested; + + static void Main(string[] args) + { + nested = args.Length > 0; + try + { + using (JobObject jo = new JobObject(nested ? "NestedJobExampleChild" : "NestedJobExampleParent")) + { + jo.Events.OnEndOfProcessTime += Events_OnEndOfProcessTime; + Process childProcess; + + if (nested) + { + + Console.WriteLine("Nested Job"); + jo.Limits.PerProcessUserTimeLimit = TimeSpan.FromMilliseconds(1000); + jo.Limits.IsKillOnJobHandleClose = true; + + System.Diagnostics.ProcessStartInfo si = + new System.Diagnostics.ProcessStartInfo("cmd.exe") + { + RedirectStandardInput = true, + UseShellExecute = false + }; + childProcess = jo.CreateProcessMayBreakAway(si); + + childProcess.StandardInput.WriteLine("@for /L %n in (1,1,1000000) do @echo %n"); + + FinishEvent.WaitOne(); + } + else + { + Console.WriteLine("Parent Job"); + jo.Limits.PerProcessUserTimeLimit = TimeSpan.FromMilliseconds(50); + jo.Limits.IsKillOnJobHandleClose = true; + + System.Diagnostics.ProcessStartInfo si = + new System.Diagnostics.ProcessStartInfo("NestedJobs.exe") + { + Arguments = "nested", + UseShellExecute = false, + }; + childProcess = jo.CreateProcessMayBreakAway(si); + FinishEvent.WaitOne(); + Console.ReadKey(); + } + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + } + + /// + /// The event handler for the OnEndOfProcessTime event + /// + /// + /// + static void Events_OnEndOfProcessTime(object sender, EndOfProcessTimeEventArgs args) + { + Console.WriteLine("Process {0} has reached its time limit (in {1} job)", args.Win32Name,nested?"child":"parent"); + FinishEvent.Set(); + } + } +} diff --git a/Examples/NestedJobs/Properties/AssemblyInfo.cs b/Examples/NestedJobs/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..dd80811 --- /dev/null +++ b/Examples/NestedJobs/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("NestedJobs")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("NestedJobs")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("89f7b595-cd09-46c7-a1db-62b4276c27a9")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/JobManagement/JobManagement.cpp b/JobManagement/JobManagement.cpp index 414095b..3c6b6e7 100644 --- a/JobManagement/JobManagement.cpp +++ b/JobManagement/JobManagement.cpp @@ -23,6 +23,7 @@ #include #include "JobException.h" #include +#include namespace JobManagement @@ -47,7 +48,10 @@ namespace JobManagement //called by the ctor void JobObject::CreateJob() { - ProbeForRunningInJob(); + if (!::IsWindows8OrGreater()) // Windows 8 added support for nested jobs + { + ProbeForRunningInJob(); + } MarshalingContext x; diff --git a/JobObjectWrapper.sln b/JobObjectWrapper.sln index a6430c6..64d9e07 100644 --- a/JobObjectWrapper.sln +++ b/JobObjectWrapper.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.9 +VisualStudioVersion = 15.0.26403.7 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{642E076D-2DD8-4C5F-A7C3-96A34E73BCFF}" EndProject @@ -52,94 +52,148 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DieOnUnhandledException", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KillAllProcessesOnJobClose", "Examples\KillAllProcessesOnJobClose\KillAllProcessesOnJobClose.csproj", "{3D3A7A91-32BB-43BF-859E-769F733F05B2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NestedJobs", "Examples\NestedJobs\NestedJobs.csproj", "{89F7B595-CD09-46C7-A1DB-62B4276C27A9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1208C948-CD63-45A9-8031-3D7EF416E623}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1208C948-CD63-45A9-8031-3D7EF416E623}.Debug|Any CPU.Build.0 = Debug|Any CPU {1208C948-CD63-45A9-8031-3D7EF416E623}.Debug|x64.ActiveCfg = Debug|x64 {1208C948-CD63-45A9-8031-3D7EF416E623}.Debug|x64.Build.0 = Debug|x64 {1208C948-CD63-45A9-8031-3D7EF416E623}.Debug|x86.ActiveCfg = Debug|x86 {1208C948-CD63-45A9-8031-3D7EF416E623}.Debug|x86.Build.0 = Debug|x86 + {1208C948-CD63-45A9-8031-3D7EF416E623}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1208C948-CD63-45A9-8031-3D7EF416E623}.Release|Any CPU.Build.0 = Release|Any CPU {1208C948-CD63-45A9-8031-3D7EF416E623}.Release|x64.ActiveCfg = Release|x64 {1208C948-CD63-45A9-8031-3D7EF416E623}.Release|x64.Build.0 = Release|x64 {1208C948-CD63-45A9-8031-3D7EF416E623}.Release|x86.ActiveCfg = Release|x86 {1208C948-CD63-45A9-8031-3D7EF416E623}.Release|x86.Build.0 = Release|x86 + {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Debug|Any CPU.ActiveCfg = Debug|Win32 {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Debug|x64.ActiveCfg = Debug|x64 {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Debug|x64.Build.0 = Debug|x64 {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Debug|x86.ActiveCfg = Debug|Win32 {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Debug|x86.Build.0 = Debug|Win32 + {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Release|Any CPU.ActiveCfg = Release|Win32 {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Release|x64.ActiveCfg = Release|x64 {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Release|x64.Build.0 = Release|x64 {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Release|x86.ActiveCfg = Release|Win32 {0E50E067-74E6-41E9-8E69-B485813CA7D8}.Release|x86.Build.0 = Release|Win32 + {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Debug|Any CPU.Build.0 = Debug|Any CPU {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Debug|x64.ActiveCfg = Debug|x64 {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Debug|x64.Build.0 = Debug|x64 {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Debug|x86.ActiveCfg = Debug|x86 {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Debug|x86.Build.0 = Debug|x86 + {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Release|Any CPU.Build.0 = Release|Any CPU {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Release|x64.ActiveCfg = Release|x64 {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Release|x64.Build.0 = Release|x64 {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Release|x86.ActiveCfg = Release|x86 {7A2E87B8-B42E-469C-BB7C-6CD132F402E5}.Release|x86.Build.0 = Release|x86 + {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Debug|Any CPU.Build.0 = Debug|Any CPU {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Debug|x64.ActiveCfg = Debug|x64 {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Debug|x64.Build.0 = Debug|x64 {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Debug|x86.ActiveCfg = Debug|x86 {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Debug|x86.Build.0 = Debug|x86 + {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Release|Any CPU.Build.0 = Release|Any CPU {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Release|x64.ActiveCfg = Release|x64 {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Release|x64.Build.0 = Release|x64 {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Release|x86.ActiveCfg = Release|x86 {C935D2E3-37E2-4906-9D5A-47B56F3BC5A8}.Release|x86.Build.0 = Release|x86 + {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Debug|Any CPU.Build.0 = Debug|Any CPU {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Debug|x64.ActiveCfg = Debug|x64 {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Debug|x64.Build.0 = Debug|x64 {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Debug|x86.ActiveCfg = Debug|x86 {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Debug|x86.Build.0 = Debug|x86 + {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Release|Any CPU.Build.0 = Release|Any CPU {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Release|x64.ActiveCfg = Release|x64 {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Release|x64.Build.0 = Release|x64 {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Release|x86.ActiveCfg = Release|x86 {CAFF6A01-8EBF-4040-9A42-E950BCBEE0F5}.Release|x86.Build.0 = Release|x86 + {32D55A47-1F54-4A6C-9B23-C89F28076530}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {32D55A47-1F54-4A6C-9B23-C89F28076530}.Debug|Any CPU.Build.0 = Debug|Any CPU {32D55A47-1F54-4A6C-9B23-C89F28076530}.Debug|x64.ActiveCfg = Debug|x64 {32D55A47-1F54-4A6C-9B23-C89F28076530}.Debug|x64.Build.0 = Debug|x64 {32D55A47-1F54-4A6C-9B23-C89F28076530}.Debug|x86.ActiveCfg = Debug|x86 {32D55A47-1F54-4A6C-9B23-C89F28076530}.Debug|x86.Build.0 = Debug|x86 + {32D55A47-1F54-4A6C-9B23-C89F28076530}.Release|Any CPU.ActiveCfg = Release|Any CPU + {32D55A47-1F54-4A6C-9B23-C89F28076530}.Release|Any CPU.Build.0 = Release|Any CPU {32D55A47-1F54-4A6C-9B23-C89F28076530}.Release|x64.ActiveCfg = Release|x64 {32D55A47-1F54-4A6C-9B23-C89F28076530}.Release|x64.Build.0 = Release|x64 {32D55A47-1F54-4A6C-9B23-C89F28076530}.Release|x86.ActiveCfg = Release|x86 {32D55A47-1F54-4A6C-9B23-C89F28076530}.Release|x86.Build.0 = Release|x86 + {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Debug|Any CPU.Build.0 = Debug|Any CPU {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Debug|x64.ActiveCfg = Debug|x64 {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Debug|x64.Build.0 = Debug|x64 {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Debug|x86.ActiveCfg = Debug|x86 {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Debug|x86.Build.0 = Debug|x86 + {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Release|Any CPU.Build.0 = Release|Any CPU {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Release|x64.ActiveCfg = Release|x64 {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Release|x64.Build.0 = Release|x64 {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Release|x86.ActiveCfg = Release|x86 {C1B24BBC-346E-4B59-8EC6-FE18375EBAF1}.Release|x86.Build.0 = Release|x86 + {54D7A119-2C07-481B-9662-BA9A65A4B766}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {54D7A119-2C07-481B-9662-BA9A65A4B766}.Debug|Any CPU.Build.0 = Debug|Any CPU {54D7A119-2C07-481B-9662-BA9A65A4B766}.Debug|x64.ActiveCfg = Debug|x64 {54D7A119-2C07-481B-9662-BA9A65A4B766}.Debug|x64.Build.0 = Debug|x64 {54D7A119-2C07-481B-9662-BA9A65A4B766}.Debug|x86.ActiveCfg = Debug|x86 {54D7A119-2C07-481B-9662-BA9A65A4B766}.Debug|x86.Build.0 = Debug|x86 + {54D7A119-2C07-481B-9662-BA9A65A4B766}.Release|Any CPU.ActiveCfg = Release|Any CPU + {54D7A119-2C07-481B-9662-BA9A65A4B766}.Release|Any CPU.Build.0 = Release|Any CPU {54D7A119-2C07-481B-9662-BA9A65A4B766}.Release|x64.ActiveCfg = Release|x64 {54D7A119-2C07-481B-9662-BA9A65A4B766}.Release|x64.Build.0 = Release|x64 {54D7A119-2C07-481B-9662-BA9A65A4B766}.Release|x86.ActiveCfg = Release|x86 {54D7A119-2C07-481B-9662-BA9A65A4B766}.Release|x86.Build.0 = Release|x86 + {1E2B4794-6273-468C-9505-C752F0BA377D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1E2B4794-6273-468C-9505-C752F0BA377D}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E2B4794-6273-468C-9505-C752F0BA377D}.Debug|x64.ActiveCfg = Debug|x64 {1E2B4794-6273-468C-9505-C752F0BA377D}.Debug|x64.Build.0 = Debug|x64 {1E2B4794-6273-468C-9505-C752F0BA377D}.Debug|x86.ActiveCfg = Debug|x86 {1E2B4794-6273-468C-9505-C752F0BA377D}.Debug|x86.Build.0 = Debug|x86 + {1E2B4794-6273-468C-9505-C752F0BA377D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1E2B4794-6273-468C-9505-C752F0BA377D}.Release|Any CPU.Build.0 = Release|Any CPU {1E2B4794-6273-468C-9505-C752F0BA377D}.Release|x64.ActiveCfg = Release|x64 {1E2B4794-6273-468C-9505-C752F0BA377D}.Release|x64.Build.0 = Release|x64 {1E2B4794-6273-468C-9505-C752F0BA377D}.Release|x86.ActiveCfg = Release|x86 {1E2B4794-6273-468C-9505-C752F0BA377D}.Release|x86.Build.0 = Release|x86 + {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Debug|Any CPU.Build.0 = Debug|Any CPU {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Debug|x64.ActiveCfg = Debug|x64 {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Debug|x64.Build.0 = Debug|x64 {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Debug|x86.ActiveCfg = Debug|x86 {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Debug|x86.Build.0 = Debug|x86 + {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Release|Any CPU.Build.0 = Release|Any CPU {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Release|x64.ActiveCfg = Release|x64 {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Release|x64.Build.0 = Release|x64 {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Release|x86.ActiveCfg = Release|x86 {3D3A7A91-32BB-43BF-859E-769F733F05B2}.Release|x86.Build.0 = Release|x86 + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Debug|x64.ActiveCfg = Debug|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Debug|x64.Build.0 = Debug|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Debug|x86.ActiveCfg = Debug|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Debug|x86.Build.0 = Debug|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Release|Any CPU.Build.0 = Release|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Release|x64.ActiveCfg = Release|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Release|x64.Build.0 = Release|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Release|x86.ActiveCfg = Release|Any CPU + {89F7B595-CD09-46C7-A1DB-62B4276C27A9}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -152,5 +206,6 @@ Global {54D7A119-2C07-481B-9662-BA9A65A4B766} = {642E076D-2DD8-4C5F-A7C3-96A34E73BCFF} {1E2B4794-6273-468C-9505-C752F0BA377D} = {642E076D-2DD8-4C5F-A7C3-96A34E73BCFF} {3D3A7A91-32BB-43BF-859E-769F733F05B2} = {642E076D-2DD8-4C5F-A7C3-96A34E73BCFF} + {89F7B595-CD09-46C7-A1DB-62B4276C27A9} = {642E076D-2DD8-4C5F-A7C3-96A34E73BCFF} EndGlobalSection EndGlobal