---
description: >-
  This feature exposes events to notify your application of system information
  updates and antivirus status changes.
---

# Events and Callbacks

## Overview

The Events feature of the SiticoneSystemInfo control allows your application to react dynamically to changes in system information. It includes events such as DeviceInfoUpdated, which signals when a full data refresh is complete, and AntivirusStatusChanged, which notifies subscribers of any changes in antivirus protection status. These events help you implement real-time UI updates and logging mechanisms in your WinForms applications.

***

### Key Points

<table><thead><tr><th width="166">Aspect</th><th>Details</th></tr></thead><tbody><tr><td>Exposed Events</td><td>DeviceInfoUpdated, AntivirusStatusChanged</td></tr><tr><td>Purpose</td><td>Notifies the application when system data is refreshed or when antivirus protection status changes</td></tr><tr><td>Integration</td><td>Allows dynamic UI updates and real-time logging or alerts based on the latest system information</td></tr></tbody></table>

***

### Best Practices

<table><thead><tr><th width="276">Practice</th><th>Description</th></tr></thead><tbody><tr><td>Subscribe Early</td><td>Subscribe to events during initialization to ensure your application receives notifications as soon as the control updates.</td></tr><tr><td>Update UI on Main Thread</td><td>Ensure that event handlers update UI components on the main thread to prevent cross-thread operation exceptions.</td></tr><tr><td>Log Event Data</td><td>Consider logging event details to aid in troubleshooting and performance monitoring.</td></tr><tr><td>Unsubscribe When Not Needed</td><td>Remove event handlers when they are no longer required to prevent memory leaks or unexpected behavior.</td></tr></tbody></table>

***

### Common Pitfalls

| Pitfall                      | Description                                                                                                               | Recommendation                                                                                   |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| Missing UI Thread Updates    | Updating UI components directly from an event handler that runs on a background thread may cause cross-thread exceptions. | Use Invoke or BeginInvoke to marshal updates to the UI thread.                                   |
| Failing to Subscribe         | Not subscribing to the events can result in missing critical updates, leaving the UI outdated.                            | Ensure that you subscribe to DeviceInfoUpdated and AntivirusStatusChanged during initialization. |
| Not Unsubscribing            | Leaving event handlers attached when the form is disposed can lead to memory leaks or unintended behavior.                | Unsubscribe from events in the form's Dispose method or when the control is no longer needed.    |
| Overcomplicating Event Logic | Implementing heavy processing inside event handlers may degrade UI responsiveness.                                        | Offload intensive processing to background tasks and keep event handlers lightweight.            |

***

### Usage Scenarios

<table><thead><tr><th width="277">Scenario</th><th>Description</th></tr></thead><tbody><tr><td>Real-Time Dashboard Updates</td><td>Update a monitoring dashboard automatically when system data is refreshed via the DeviceInfoUpdated event.</td></tr><tr><td>Antivirus Alert System</td><td>Trigger an alert or update a status indicator when the AntivirusStatusChanged event signals a change in protection status.</td></tr><tr><td>Diagnostic Logging</td><td>Log details of system refresh events for audit trails or troubleshooting purposes.</td></tr><tr><td>Dynamic UI Refresh</td><td>Refresh UI components dynamically upon receiving event notifications to reflect the most current system configuration data.</td></tr></tbody></table>

***

### Real Life Usage Scenarios

<table><thead><tr><th width="232">Scenario</th><th>Description</th></tr></thead><tbody><tr><td>IT Monitoring Dashboard</td><td>Integrate events into a real-time dashboard that alerts IT administrators when system information or antivirus status changes.</td></tr><tr><td>Support Tools</td><td>Build a diagnostic tool that logs every time the system data is refreshed, aiding support staff in identifying patterns during troubleshooting.</td></tr><tr><td>Compliance Auditing</td><td>Automatically update compliance reports whenever the system data is updated, ensuring that audits always reflect the latest state.</td></tr></tbody></table>

***

### Troubleshooting Tips

<table><thead><tr><th width="242">Tip</th><th>Description</th><th>Recommendation</th></tr></thead><tbody><tr><td>Ensure UI Thread Invocation</td><td>Event handlers may execute on a non-UI thread, causing exceptions when updating UI controls.</td><td>Use Invoke or BeginInvoke in your event handlers to ensure UI updates are performed on the main thread.</td></tr><tr><td>Verify Event Subscriptions</td><td>If expected updates are not occurring, the events may not be properly subscribed.</td><td>Double-check that your event subscriptions are set up during initialization and remain active throughout the control’s lifecycle.</td></tr><tr><td>Log Event Data</td><td>Undiagnosed issues may arise if events are not firing as expected.</td><td>Implement logging in your event handlers to verify that events are being raised and to capture event data for debugging.</td></tr><tr><td>Unsubscribe When Appropriate</td><td>Memory leaks or unexpected behavior can occur if event handlers remain subscribed after the control is disposed.</td><td>Ensure proper unsubscription in the Dispose method or when the associated UI component is no longer in use.</td></tr></tbody></table>

***

### Code Examples & Integration Demos

#### Example 1: Subscribing to DeviceInfoUpdated Event

**Description:** Subscribe to the DeviceInfoUpdated event to update UI components when system information is refreshed.

```csharp
// In your form initialization
public MainForm()
{
    InitializeComponent();
    systemInfoControl.DeviceInfoUpdated += SystemInfoControl_DeviceInfoUpdated;
}

private void SystemInfoControl_DeviceInfoUpdated(object sender, DeviceInfoUpdateEventArgs e)
{
    // Marshal the UI update to the main thread
    Invoke(new Action(() =>
    {
        // Update UI elements, for example, refresh a data grid view
        dataGridViewSystemInfo.DataSource = systemInfoControl.Services.ToList();
    }));
}
```

#### Example 2: Handling AntivirusStatusChanged Event

**Description:** Subscribe to the AntivirusStatusChanged event to update an antivirus status label on the UI.

```csharp
// In your form initialization
public MainForm()
{
    InitializeComponent();
    systemInfoControl.AntivirusStatusChanged += SystemInfoControl_AntivirusStatusChanged;
}

private void SystemInfoControl_AntivirusStatusChanged(object sender, AntivirusStatusEventArgs e)
{
    // Update UI on the main thread
    Invoke(new Action(() =>
    {
        var status = e.Status;
        lblAntivirusStatus.Text = status.IsProtectionEnabled 
            ? (status.IsUpToDate ? "Antivirus Active & Up-to Date" : "Antivirus Active but Outdated") 
            : "No Antivirus Detected";
    }));
}
```

#### Example 3: Unsubscribing from Events

**Description:** Unsubscribe from events when the form is closing to prevent memory leaks.

```csharp
protected override void OnFormClosing(FormClosingEventArgs e)
{
    // Unsubscribe from events to prevent memory leaks
    systemInfoControl.DeviceInfoUpdated -= SystemInfoControl_DeviceInfoUpdated;
    systemInfoControl.AntivirusStatusChanged -= SystemInfoControl_AntivirusStatusChanged;
    base.OnFormClosing(e);
}
```

***

### Review

<table><thead><tr><th width="215">Aspect</th><th>Review Comments</th></tr></thead><tbody><tr><td>Integration Simplicity</td><td>Events are straightforward to subscribe to and provide real-time notifications, simplifying dynamic UI updates and logging mechanisms.</td></tr><tr><td>Robustness</td><td>The event-driven approach enhances application responsiveness, but proper error handling and thread marshaling are essential to avoid UI-related issues.</td></tr><tr><td>UI Flexibility</td><td>Events facilitate a wide range of integrations, from updating dashboards to triggering alerts, making them highly adaptable to various application needs.</td></tr><tr><td>Documentation Clarity</td><td>Clear examples and best practices ensure developers can quickly understand how to implement event handlers without exposing internal logic.</td></tr></tbody></table>

***

### Summary

The Events feature in the SiticoneSystemInfo control enables your application to react dynamically to system information updates and antivirus status changes. By subscribing to events such as DeviceInfoUpdated and AntivirusStatusChanged, you can implement real-time UI updates, logging, and alerting. Follow the provided best practices and code examples to ensure that your event handlers operate on the UI thread and that you manage subscriptions appropriately. Happy coding!
