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 : IDisposable

Example

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

NameDescription
RebootReasonGets the reason a system restart is required to free the registered resources.
SessionKeyGets the session key for this Restart Manager session.

RebootReason

RestartManager.cs

Gets the reason a system restart is required to free the registered resources.

public RestartManagerRebootReason RebootReason { get; }

Property value

RestartManagerRebootReason

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.

Gets the session key for this Restart Manager session.

public string SessionKey { get; }

Property value

string

Methods

NameDescription
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.cs

Cancels 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.cs

Creates 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.

Ends the Restart Manager session and releases all resources. Calling this method more than once has no effect.

public void Dispose()

GetLockingProcesses()

RestartManager.cs

Gets 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.cs

Gets a list of processes that are currently locking the specified file.

public static IReadOnlyList<Process> GetProcessesLockingFile(string path)

Parameters

path string
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.cs

Gets a list of processes that are currently locking any of the specified files.

public static IReadOnlyList<Process> GetProcessesLockingFiles(string[] paths)

Parameters

paths string[]
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 paths is null.
ArgumentException
Thrown when paths contains a null element.
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.cs

Gets 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.cs

Determines whether the specified file is currently locked by any process.

public static bool IsFileLocked(string path)

Parameters

path string
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.cs

Determines 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.cs

Joins an existing Restart Manager session using the specified session key.

public static RestartManager JoinSession(string sessionKey)

Parameters

sessionKey string
The session key of an existing Restart Manager session.

Returns

RestartManager A RestartManager instance representing the joined session.

Exceptions

ArgumentNullException
Thrown when sessionKey is null.
Win32Exception
Thrown when joining the session fails.

RegisterFile(string)

RestartManager.cs

Registers a file to be monitored by the Restart Manager session.

public void RegisterFile(string path)

Parameters

path string
The full path of the file to register.

Exceptions

ArgumentNullException
Thrown when path is null.
Win32Exception
Thrown when the registration fails.

RegisterFiles(string[])

RestartManager.cs

Registers multiple files to be monitored by the Restart Manager session.

public void RegisterFiles(string[] paths)

Parameters

paths string[]
An array of full file paths to register.

Exceptions

ArgumentNullException
Thrown when paths is null.
ArgumentException
Thrown when paths contains a null element.
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

Restarts 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.cs

Restarts applications and services that were shut down by the Restart Manager and that were registered for restart.

public void Restart(RestartManagerWriteStatusCallback? statusCallback)
Parameters
statusCallback RestartManagerWriteStatusCallback?
An optional callback to receive progress updates during the restart operation. The callback is invoked by native code and must not throw; use RestartManager.CancelCurrentTask from another thread to stop the operation instead.
Exceptions
Win32Exception
Thrown when the restart operation fails.

Shutdown overloads

Shutdown(RestartManagerShutdownType)

RestartManager.cs

Shuts down applications and services that are using the registered resources.

public void Shutdown(RestartManagerShutdownType action)
Parameters
action RestartManagerShutdownType
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.cs

Shuts down applications and services that are using the registered resources.

public void Shutdown(RestartManagerShutdownType action, RestartManagerWriteStatusCallback? statusCallback)
Parameters
action RestartManagerShutdownType
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.
statusCallback RestartManagerWriteStatusCallback?
An optional callback to receive progress updates during the shutdown operation. The callback is invoked by native code and must not throw; use RestartManager.CancelCurrentTask from another thread to stop the operation instead.
Exceptions
Win32Exception
Thrown when the shutdown operation fails.