RestartManager Class
- Supported on windows6.0.6000
Provides a wrapper around the Windows Restart Manager API to detect which processes are locking files and manage application restarts.
[SupportedOSPlatform("windows6.0.6000")]
public sealed class RestartManager : IDisposableExample
var path = Path.GetTempFileName();
// The file is locked while this stream is open
using var stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
// Check if a file is locked
Assert.True(RestartManager.IsFileLocked(path));
// Get processes locking a file. The caller owns the returned Process instances.
var processes = RestartManager.GetProcessesLockingFile(path);
foreach (var process in processes)
{
Assert.Equal(Environment.ProcessId, process.Id);
process.Dispose();
}
// Manual session management
using var session = RestartManager.CreateSession();
session.RegisterFile(path);
if (session.IsResourcesLocked())
{
foreach (var process in session.GetProcessesLockingResources())
{
// Handle locked resources
process.Dispose();
}
}- Implements
Properties
| Name | Description |
|---|---|
| RebootReason | Gets the reason a system restart is required to free the registered resources. |
| SessionKey | Gets the session key for this Restart Manager session. |
RebootReason
RestartManager.csGets the reason a system restart is required to free the registered resources.
public RestartManagerRebootReason RebootReason { get; }Property value
Remarks
This reflects the most recent call to RestartManager.IsResourcesLocked, RestartManager.GetProcessesLockingResources or RestartManager.GetLockingProcesses, and is RestartManagerRebootReason.None until one of them runs. When it is not RestartManagerRebootReason.None, calling RestartManager.Shutdown will not free the resources and the user should be prompted for a system restart instead.
SessionKey
RestartManager.csMethods
| Name | Description |
|---|---|
| CancelCurrentTask() | Cancels the RestartManager.Shutdown or RestartManager.Restart operation that is currently running on this session. |
| CreateSession() | Creates a new Restart Manager session. |
| Dispose() | Ends the Restart Manager session and releases all resources. Calling this method more than once has no effect. |
| GetLockingProcesses() | Gets the applications and services that are currently using the registered resources. |
| GetProcessesLockingFile(string) | Gets a list of processes that are currently locking the specified file. |
| GetProcessesLockingFiles(string[]) | Gets a list of processes that are currently locking any of the specified files. |
| GetProcessesLockingResources() | Gets a list of processes that are currently locking the registered resources. |
| IsFileLocked(string) | Determines whether the specified file is currently locked by any process. |
| IsResourcesLocked() | Determines whether any of the registered resources are currently locked by running processes. |
| JoinSession(string) | Joins an existing Restart Manager session using the specified session key. |
| RegisterFile(string) | Registers a file to be monitored by the Restart Manager session. |
| RegisterFiles(string[]) | Registers multiple files to be monitored by the Restart Manager session. |
| Restart() | Restarts applications and services that were shut down by the Restart Manager and that were registered for restart. |
| Restart(RestartManagerWriteStatusCallback?) | Restarts applications and services that were shut down by the Restart Manager and that were registered for restart. |
| Shutdown(RestartManagerShutdownType) | Shuts down applications and services that are using the registered resources. |
| Shutdown(RestartManagerShutdownType, RestartManagerWriteStatusCallback?) | Shuts down applications and services that are using the registered resources. |
CancelCurrentTask()
RestartManager.csCancels the RestartManager.Shutdown or RestartManager.Restart operation that is currently running on this session.
public void CancelCurrentTask()Exceptions
Win32Exception- Thrown when the cancellation fails.
Remarks
RestartManager.Shutdown and RestartManager.Restart block until they complete, so this method has to be called from another thread while one of them is running. It can only be called by the session that was created with RestartManager.CreateSession, not by a session joined with RestartManager.JoinSession.
CreateSession()
RestartManager.csCreates a new Restart Manager session.
public static RestartManager CreateSession()Returns
RestartManager A new RestartManager instance representing the session.
Exceptions
Win32Exception- Thrown when the session creation fails.
Dispose()
RestartManager.csEnds the Restart Manager session and releases all resources. Calling this method more than once has no effect.
public void Dispose()GetLockingProcesses()
RestartManager.csGets the applications and services that are currently using the registered resources.
public IReadOnlyList<RestartManagerProcessInfo> GetLockingProcesses()Returns
IReadOnlyList<RestartManagerProcessInfo> A read-only list of RestartManagerProcessInfo describing each application or service.
Exceptions
Win32Exception- Thrown when the operation fails.
Remarks
Unlike RestartManager.GetProcessesLockingResources, this method reports everything the Restart Manager knows about each application, including its name, type, status and whether it can be restarted, and it also reports applications that have already exited.
GetProcessesLockingFile(string)
RestartManager.csGets a list of processes that are currently locking the specified file.
public static IReadOnlyList<Process> GetProcessesLockingFile(string path)Parameters
pathstring- The full path of the file to check.
Returns
IReadOnlyList<Process> A read-only list of Process instances that are locking the file. The caller owns the returned instances and should dispose them.
Exceptions
Win32Exception- Thrown when the operation fails.
GetProcessesLockingFiles(string[])
RestartManager.csGets a list of processes that are currently locking any of the specified files.
public static IReadOnlyList<Process> GetProcessesLockingFiles(string[] paths)Parameters
pathsstring[]- An array of full file paths to check.
Returns
IReadOnlyList<Process> A read-only list of Process instances that are locking at least one of the files. The caller owns the returned instances and should dispose them.
Exceptions
ArgumentNullException- Thrown when
pathsisnull. ArgumentException- Thrown when
pathscontains anullelement. Win32Exception- Thrown when the operation fails.
Remarks
Prefer this method over calling RestartManager.GetProcessesLockingFile in a loop: registering resources performs relatively expensive write operations, so registering all the files in a single session is significantly cheaper, though a session can only hold a bounded number of paths - see RestartManager.RegisterFiles.
GetProcessesLockingResources()
RestartManager.csGets a list of processes that are currently locking the registered resources.
public IReadOnlyList<Process> GetProcessesLockingResources()Returns
IReadOnlyList<Process> A read-only list of Process instances that are locking the registered resources. The caller owns the returned instances and should dispose them.
Exceptions
Win32Exception- Thrown when the operation fails.
IsFileLocked(string)
RestartManager.csDetermines whether the specified file is currently locked by any process.
Parameters
pathstring- The full path of the file to check.
Returns
bool true if the file is locked; otherwise, false.
Exceptions
Win32Exception- Thrown when the operation fails.
IsResourcesLocked()
RestartManager.csDetermines whether any of the registered resources are currently locked by running processes.
public bool IsResourcesLocked()Returns
bool true if at least one registered resource is locked; otherwise, false.
Exceptions
Win32Exception- Thrown when the operation fails.
JoinSession(string)
RestartManager.csJoins an existing Restart Manager session using the specified session key.
public static RestartManager JoinSession(string sessionKey)Parameters
sessionKeystring- The session key of an existing Restart Manager session.
Returns
RestartManager A RestartManager instance representing the joined session.
Exceptions
ArgumentNullException- Thrown when
sessionKeyisnull. Win32Exception- Thrown when joining the session fails.
RegisterFile(string)
RestartManager.csRegisters a file to be monitored by the Restart Manager session.
Parameters
pathstring- The full path of the file to register.
Exceptions
ArgumentNullException- Thrown when
pathisnull. Win32Exception- Thrown when the registration fails.
RegisterFiles(string[])
RestartManager.csRegisters multiple files to be monitored by the Restart Manager session.
Parameters
pathsstring[]- An array of full file paths to register.
Exceptions
ArgumentNullException- Thrown when
pathsisnull. ArgumentException- Thrown when
pathscontains anullelement. Win32Exception- Thrown when the registration fails.
Remarks
The Restart Manager persists registrations to the registry, so a single session can only hold a bounded number of paths. Registering a very large set fails with a Win32Exception for ERROR_WRITE_FAULT (29) rather than a dedicated error; split such sets across several sessions.
Restart overloads
Restart()
RestartManager.csRestarts applications and services that were shut down by the Restart Manager and that were registered for restart.
public void Restart()Exceptions
Win32Exception- Thrown when the restart operation fails.
Restart(RestartManagerWriteStatusCallback?)
RestartManager.csRestarts applications and services that were shut down by the Restart Manager and that were registered for restart.
public void Restart(RestartManagerWriteStatusCallback? statusCallback)Parameters
statusCallbackRestartManagerWriteStatusCallback?- An optional callback to receive progress updates during the restart operation. The callback is invoked by native code and must not throw; use
RestartManager.CancelCurrentTaskfrom another thread to stop the operation instead.
Exceptions
Win32Exception- Thrown when the restart operation fails.
Shutdown overloads
Shutdown(RestartManagerShutdownType)
RestartManager.csShuts down applications and services that are using the registered resources.
public void Shutdown(RestartManagerShutdownType action)Parameters
actionRestartManagerShutdownType- The shutdown options to use. The Restart Manager documents this parameter as taking one or more of the defined options, so pass at least one flag.
Exceptions
Win32Exception- Thrown when the shutdown operation fails.
Shutdown(RestartManagerShutdownType, RestartManagerWriteStatusCallback?)
RestartManager.csShuts down applications and services that are using the registered resources.
public void Shutdown(RestartManagerShutdownType action, RestartManagerWriteStatusCallback? statusCallback)Parameters
actionRestartManagerShutdownType- The shutdown options to use. The Restart Manager documents this parameter as taking one or more of the defined options, so pass at least one flag.
statusCallbackRestartManagerWriteStatusCallback?- An optional callback to receive progress updates during the shutdown operation. The callback is invoked by native code and must not throw; use
RestartManager.CancelCurrentTaskfrom another thread to stop the operation instead.
Exceptions
Win32Exception- Thrown when the shutdown operation fails.