Suppress New Processes Initiated by .NET Controls
- 3 minutes to read
DevExpress UI controls can initiate new processes in response to user actions. For example, DevExpress RichEdit or HyperlinkEdit controls can open a system’s default browser when a user clicks a hyperlink. Use the DevExpress.Data.Utils.ProcessStartPolicy class to apply a policy that manages processes.
Process Start Policies
To apply a policy, call one of the following methods of the DevExpress.Data.Utils.ProcessStartPolicy
class at application startup:
- SuppressAll() - Applies a policy that suppresses all new processes.
- ThrowAlways() - Applies a policy that throws an exception when a process is about to start.
- RequireConfirmation() - Applies a policy that displays a confirmation dialog that prompts a user to confirm or cancel a process.
If none of these methods are called, DevExpress UI controls allow all processes to start. All exceptions that indicate failed processes are suppressed.
The following code demonstrates how to suppress all user-invoked processes in a Windows Forms app:
static void Main() {
DevExpress.Data.Utils.ProcessStartPolicy.SuppressAll();
// ...
Application.Run(new Form1());
}
Trusted Resources
The ThrowAlways
and RequireConfirmation
policies allow you to setup trusted documents, commands, or URLs. Processes for trusted resources bypass exceptions or confirmation dialogs.
To register a trusted resource, use the static RegisterTrustedProcess method:
DevExpress.Data.Utils.ProcessStartPolicy.RegisterTrustedProcess("https://www.devexpress.com");
Process Start Events
Handle the following events of the ProcessStartPolicy
class to perform custom actions when a DevExpress UI control starts a process:
Starting – Fires before a process starts and allows you to cancel the process.
static void Main() { ProcessStartPolicy.Starting += ProcessStartPolicy_Starting; // ... } private static void ProcessStartPolicy_Starting(object sender, System.ComponentModel.CancelEventArgs e) { ProcessStartInfo psi = sender as ProcessStartInfo; e.Cancel = !psi.FileName.Contains("www.devexpress.com"); }
- Started – Fires after a process has started. This is a notification event.
Process Start Failures
Exceptions are not raised if a process fails to start. To throw exceptions in this instance, call the static ThrowOnErrors method at application startup.
You can also handle the Failed event to respond to associated failures. The following example demonstrates how to suppress an internal exception and display a message box:
static void Main() {
DevExpress.Data.Utils.ProcessStartPolicy.Failed += ProcessStartPolicy_Failed;
//...
}
private static void ProcessStartPolicy_Failed(object sender, DevExpress.Data.Utils.ProcessStartPolicy.ProcessStartFailedExceptionEventArgs e) {
if(e.Exception is Win32Exception) {
System.Diagnostics.ProcessStartInfo si = sender as System.Diagnostics.ProcessStartInfo;
e.Throw = false;
MessageBox.Show("File not found: " + si.FileName);
}
}