AssignProcessToJobObject无法将进程添加到作业对象 [英] AssignProcessToJobObject fails to add process to job object
问题描述
您好!
我正在努力使用JobObject(C#/ Win7),以便以最简单的方式使其工作。
I am struggling with JobObject (C#/Win7) to make it work even in the simplest way.
目标是启动一些进程,将它们放入一个作业,然后调用TerminateJobObject()来结束所有进程。
The goal is to start a few processes, put them into one job and then call TerminateJobObject() to end all the processes.
根据文档,如果使用InPtr.Zero - 默认JobObject的属性是允许/支持终止。
According to the docs if InPtr.Zero is used - the default properties for the JobObject is that termination is allowed/supported.
代码非常简单,AddProcessToJobObject失败。
The code is very simple, AddProcessToJobObject fails.
private IntPtr handle;
public MyJobObject()
{
handle = CreateJobObject(IntPtr.Zero, null);
}
public bool AddProcess(int processId)
{
bool r = AssignProcessToJobObject(handle, Process.GetProcessById(processId).Handle);
return r;
}
public bool Terminate()
{
bool r = TerminateJobObject(handle, 1);
CloseHandle(handle);
return r;
}
创建流程很简单:
The process creation is simple:
public static int StartOne()
{
Process process = new System.Diagnostics.Process();
// ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", "/K");
ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe", "");
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = false;
process.StartInfo = startInfo;
process.Start();
return process.Id;
}
并且有一个简单的WinForm应用程序可以使用3个按钮进行测试:
and there is a simple WinForm app to test this with 3 buttons:
private void buttonOpenJob_Click(object sender, EventArgs e)
{
m_Job = new MyJobObject();
int pid = Process.GetCurrentProcess().Id;
}
private void buttonStartProcess_Click(object sender, EventArgs e)
{
if (m_Job != null)
{
int proc = MyProcesses.StartOne();
m_Job.AddProcess(proc);
}
}
private void buttonCloseJob_Click(object sender, EventArgs e)
{
if (m_Job != null)
{
m_Job.Terminate();
m_Job = null;
}
}
那么我错过了什么?
$
它是在JobObject端还是进程 - 创作方?
So what did I miss?
Is it on the JobObject side or the process-creation side?
提前致谢!
Pete
推荐答案
没人?我认为这是一个非常好的和强大的功能 - 只是没能使它工作...
Nobody? I thought that this is a very nice and powerful feature - just failed to make it work...
这篇关于AssignProcessToJobObject无法将进程添加到作业对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!