本文整理汇总了C#中ProcessHandle.Terminate方法的典型用法代码示例。如果您正苦于以下问题:C# ProcessHandle.Terminate方法的具体用法?C# ProcessHandle.Terminate怎么用?C# ProcessHandle.Terminate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ProcessHandle
的用法示例。
在下文中一共展示了ProcessHandle.Terminate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: restartProcessMenuItem_Click
private void restartProcessMenuItem_Click(object sender, EventArgs e)
{
if (PhUtils.ShowConfirmMessage(
"restart",
"the selected process",
"The process will be restarted with the same command line and " +
"working directory, but if it is running under a different user it " +
"will be restarted under the current user.",
true
))
{
try
{
using (var phandle = new ProcessHandle(processSelectedPid,
Program.MinProcessQueryRights | Program.MinProcessReadMemoryRights))
{
string currentDirectory = phandle.GetPebString(PebOffset.CurrentDirectoryPath);
string cmdLine = phandle.GetPebString(PebOffset.CommandLine);
try
{
using (var phandle2 = new ProcessHandle(processSelectedPid, ProcessAccess.Terminate))
phandle2.Terminate();
}
catch (Exception ex)
{
PhUtils.ShowException("Unable to terminate the process", ex);
return;
}
try
{
var startupInfo = new StartupInfo();
var procInfo = new ProcessInformation();
startupInfo.Size = Marshal.SizeOf(startupInfo);
if (!Win32.CreateProcess(null, cmdLine, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, currentDirectory,
ref startupInfo, out procInfo))
Win32.ThrowLastError();
Win32.CloseHandle(procInfo.ProcessHandle);
Win32.CloseHandle(procInfo.ThreadHandle);
}
catch (Exception ex)
{
PhUtils.ShowException("Unable to start the command '" + cmdLine + "'", ex);
}
}
}
catch (Exception ex)
{
PhUtils.ShowException("Unable to restart the process", ex);
}
}
}
示例2: TP3
private void TP3()
{
using (ProcessHandle phandle = new ProcessHandle(_pid, Program.MinProcessQueryRights))
{
phandle.Terminate();
}
}
示例3: Terminate
public static bool Terminate(IWin32Window window, int[] pids, string[] names, bool prompt)
{
bool allGood = true;
if (ElevateIfRequired(window, pids, names, ProcessAccess.Terminate, "terminate"))
return false;
if (prompt && !Prompt(window, pids, names, "terminate",
"Terminating a process will cause unsaved data to be lost. " +
"Terminating a system process will cause system instability. " +
"Are you sure you want to continue?", false))
return false;
for (int i = 0; i < pids.Length; i++)
{
try
{
using (ProcessHandle phandle =
new ProcessHandle(pids[i], ProcessAccess.Terminate))
phandle.Terminate();
}
catch (Exception ex)
{
allGood = false;
if (!PhUtils.ShowContinueMessage(
"Unable to terminate " + GetName(pids, names, i),
ex
))
return false;
}
}
return allGood;
}
示例4: TerminateTree
private static bool TerminateTree(IWin32Window window, Dictionary<int, SystemProcess> processes, int pid)
{
bool good = true;
foreach (var process in processes)
{
if (process.Value.Process.ProcessId < 4)
continue;
if (process.Value.Process.InheritedFromProcessId.Equals(pid))
if (!TerminateTree(window, processes, process.Value.Process.ProcessId))
good = false;
}
try
{
using (ProcessHandle phandle =
new ProcessHandle(pid, ProcessAccess.Terminate))
phandle.Terminate();
}
catch (Exception ex)
{
good = false;
PhUtils.ShowException(
"Unable to terminate the process \"" + processes[pid].Name + "\" with PID " + pid.ToString(),
ex
);
}
return good;
}
示例5: restartProcessMenuItem_Click
private void restartProcessMenuItem_Click(object sender, EventArgs e)
{
if (PhUtils.ShowConfirmMessage(
"restart",
"the selected process",
"The process will be restarted with the same command line and " +
"working directory, but if it is running under a different user it " +
"will be restarted under the current user.",
true
))
{
try
{
using (var phandle = new ProcessHandle(processSelectedPid,
Program.MinProcessQueryRights | Program.MinProcessReadMemoryRights))
{
string currentDirectory = phandle.GetPebString(PebOffset.CurrentDirectoryPath);
string cmdLine = phandle.GetPebString(PebOffset.CommandLine);
try
{
using (var phandle2 = new ProcessHandle(processSelectedPid, ProcessAccess.Terminate))
phandle2.Terminate();
}
catch (Exception ex)
{
PhUtils.ShowException("Unable to terminate the process", ex);
return;
}
try
{
ClientId cid;
ThreadHandle thandle;
ProcessHandle.CreateWin32(
null,
cmdLine,
false,
0,
EnvironmentBlock.Zero,
currentDirectory,
new StartupInfo(),
out cid,
out thandle
).Dispose();
thandle.Dispose();
}
catch (Exception ex)
{
PhUtils.ShowException("Unable to start the command '" + cmdLine + "'", ex);
}
}
}
catch (Exception ex)
{
PhUtils.ShowException("Unable to restart the process", ex);
}
}
}