Suppress New Processes Initiated by .NET Controls
- 3 minutes to read
DevExpress controls can start new processes in response to user actions. For example, the RichEdit or HyperlinkEdit controls can open the system’s default browser when a user clicks a hyperlink. If you use v19.1 or later, you can 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 shows a confirmation dialog that prompts a user to confirm or cancel a process.
If none of these methods is called, DevExpress controls allow all processes to start. All exceptions that indicate failed processes are suppressed.
The following code suppresses all user-invoked processes:
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
To perform custom actions when a DevExpress control starts a process, use the following two events of the ProcessStartPolicy class:
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 is started. This is a notification event.
Process Start Failures
Exceptions are not raised if a process fails to start. To throw exceptions in this case, call the static ThrowOnErrors method at application startup. You can also handle the Failed event to respond to these failures. The following code handles the Failed event 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);
}
}