2013-02-19 29 views
0

我試圖設置「job_object_limit_kill_on_job_close」一個子進程使用這個:AssignProcessToJobObject不分配正確

int pid = System.Diagnostics.Process.GetProcessesByName("Child")[0].Id; 
Job job = new Job("TEST JOB"); 
job.AddProcess(Process.GetProcessById((int)pid).Handle); 

使用Process Explorer中我看到的工作「測試作業」分配給我的孩子的過程,但沒有工作限制以某種方式列出......它只是空的。

這是什麼原因造成的?

GetLastWin32Error只是返回0

下面是我使用的類:

public class Job : IDisposable 
{ 
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] 
    static extern IntPtr CreateJobObject(IntPtr a, string lpName); 

    [DllImport("kernel32.dll")] 
    static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, UInt32 cbJobObjectInfoLength); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process); 

    private IntPtr handle; 
    private bool disposed; 

    public Job(string jobName) 
    { 
     handle = CreateJobObject(IntPtr.Zero, jobName); 

     var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION 
     { 
      LimitFlags = 0x2000 
     }; 

     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(handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length)) 
      throw new Exception(string.Format("Unable to set information. Error: {0}", Marshal.GetLastWin32Error())); 
    } 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 

    private void Dispose(bool disposing) 
    { 
     if (disposed) 
      return; 

     if (disposing) { } 

     Close(); 
     disposed = true; 
    } 

    [DllImport("kernel32.dll", SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool CloseHandle(IntPtr hObject); 

    public void Close() 
    { 
     CloseHandle(handle); 
     handle = IntPtr.Zero; 
    } 

    public bool AddProcess(IntPtr processHandle) 
    { 
     return AssignProcessToJobObject(handle, processHandle); 
    } 

    public bool AddProcess(int processId) 
    { 
     return AddProcess(Process.GetProcessById(processId).Handle); 
    } 

} 

#region Helper classes 

[StructLayout(LayoutKind.Sequential)] 
struct IO_COUNTERS 
{ 
    public UInt64 ReadOperationCount; 
    public UInt64 WriteOperationCount; 
    public UInt64 OtherOperationCount; 
    public UInt64 ReadTransferCount; 
    public UInt64 WriteTransferCount; 
    public UInt64 OtherTransferCount; 
} 


[StructLayout(LayoutKind.Sequential)] 
struct JOBOBJECT_BASIC_LIMIT_INFORMATION 
{ 
    public Int64 PerProcessUserTimeLimit; 
    public Int64 PerJobUserTimeLimit; 
    public UInt32 LimitFlags; 
    public UIntPtr MinimumWorkingSetSize; 
    public UIntPtr MaximumWorkingSetSize; 
    public UInt32 ActiveProcessLimit; 
    public UIntPtr Affinity; 
    public UInt32 PriorityClass; 
    public UInt32 SchedulingClass; 
} 

[StructLayout(LayoutKind.Sequential)] 
public struct SECURITY_ATTRIBUTES 
{ 
    public UInt32 nLength; 
    public IntPtr lpSecurityDescriptor; 
    public Int32 bInheritHandle; 
} 

[StructLayout(LayoutKind.Sequential)] 
struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION 
{ 
    public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation; 
    public IO_COUNTERS IoInfo; 
    public UIntPtr ProcessMemoryLimit; 
    public UIntPtr JobMemoryLimit; 
    public UIntPtr PeakProcessMemoryUsed; 
    public UIntPtr PeakJobMemoryUsed; 
} 

public enum JobObjectInfoType 
{ 
    AssociateCompletionPortInformation = 7, 
    BasicLimitInformation = 2, 
    BasicUIRestrictions = 4, 
    EndOfJobTimeInformation = 6, 
    ExtendedLimitInformation = 9, 
    SecurityLimitInformation = 5, 
    GroupInformation = 11 
} 

#endregion 
+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2013-02-19 14:07:47

+0

好吧,對不起:S新在這裏 – Saschanski 2013-02-19 14:16:00

回答

0

沒有什麼是錯的,進程資源管理器只是不顯示在結束標誌殺。它會顯示的是job memory and process limits

您是否測試並確保您的子流程在您結束工作時結束?只要你的程序行爲正確,你就不用擔心外部工具正在顯示什麼。

+0

nope沒有工作。客戶端過程仍然是爲什麼。但TY告訴我,進程瀏覽器問題:) – Saschanski 2013-02-19 16:48:52