2018-02-14 15:26:05 +01:00
|
|
|
|
/*
|
2019-01-09 11:25:21 +01:00
|
|
|
|
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
|
2018-02-14 15:26:05 +01:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.WindowsApi.Contracts;
|
|
|
|
|
using SafeExamBrowser.WindowsApi.Contracts.Events;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.WindowsApi
|
|
|
|
|
{
|
|
|
|
|
internal class Process : IProcess
|
|
|
|
|
{
|
|
|
|
|
private System.Diagnostics.Process process;
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
public event ProcessTerminatedEventHandler Terminated;
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
public int Id
|
|
|
|
|
{
|
|
|
|
|
get { return process.Id; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
public bool HasTerminated
|
|
|
|
|
{
|
2018-09-25 12:10:34 +02:00
|
|
|
|
get { process.Refresh(); return process.HasExited; }
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
public Process(int id)
|
|
|
|
|
{
|
|
|
|
|
process = System.Diagnostics.Process.GetProcessById(id);
|
2018-02-20 15:15:26 +01:00
|
|
|
|
process.Exited += Process_Exited;
|
|
|
|
|
process.EnableRaisingEvents = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Kill()
|
|
|
|
|
{
|
2018-09-25 12:10:34 +02:00
|
|
|
|
process.Refresh();
|
|
|
|
|
|
|
|
|
|
if (!process.HasExited)
|
|
|
|
|
{
|
|
|
|
|
process.Kill();
|
|
|
|
|
}
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Process_Exited(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Terminated?.Invoke(process.ExitCode);
|
2018-02-14 15:26:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|