{"title":"Subscribing to Events in a .NET Framework Type","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-dotnet-subscribe-to-events/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-dotnet-subscribe-to-events, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-dotnet-subscribe-to-events","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"How to subscribe to event in a .NET Framework Type","date":"2021-04-01","meta":{"topic":"concept-article"},"outline":[{"level":1,"title":"Subscribing to Events in a .NET Framework Type","slug":"subscribing-to-events-in-a-net-framework-type","at":0,"chars":2304},{"level":2,"title":"Related information","slug":"related-information","at":1838,"chars":466}],"text":"# Subscribing to Events in a .NET Framework Type\nWith .NET Framework interoperability in Dynamics 365 Business Central objects, you can configure a DotNet variable to subscribe to events that are published by a .NET Framework type. Events are handled by triggers in the AL code of the Business Central object.\nYou start by declaring in AL the usage of two .NET types from the System assembly. The first type is System.Timers.Timer and it will be used to generate .NET events. The second one is called System.Timers.ElapsedEventArgs and it is required for creating a subscriber to the Elapsed event emitted by the Timer type.\n```al\ndotnet\n{\n    assembly(System)\n    {\n        type(System.Timers.Timer; MyTimer) {}\n        type(System.Timers.ElapsedEventArgs; MyElapsedEventArgs) {}\n    }\n}\n```\nYou can only subscribe to events that are emitted by global variables of the .NET type marked with the WithEvents attribute.\nFor all the global variables that are marked with this attribute, the compiler will expose the events available on the type as triggers on the variable.\nThe syntax for declaring these triggers is `{VariableName}::{EventName}(...ParameterList)`, but IntelliSense will offer suggestions for the event name and autocomplete the parameter list.\n```al\npageextension 50101 CustomerListExt extends \"Customer List\"\n{\n    var\n        [WithEvents]\n        timer: DotNet MyTimer;\n\n    trigger OnOpenPage()\n    begin\n        SetupTimer();\n    end;\n\n    procedure SetupTimer()\n    begin\n        timer := timer.Timer(2000);\n        timer.AutoReset := true;\n        timer.Enabled := true;\n        timer.Start();\n    end;\n\n    trigger timer::Elapsed(sender: Variant; e: DotNet MyElapsedEventArgs)\n    begin\n     // Print a message when this event is published\n        Message('%1', e.SignalTime());\n        timer.Stop();\n    end;\n}\n```\n## Related information\nGetting started with Microsoft .NET Interoperability from AL<devenv-get-started-call-dotnet-from-al>\nMigrating from .NET Framework to .NET Standard<devenv-migrate-from-dotnet-framework-to-dotnet-standard>\n.NET Control Add-Ins<devenv-dotnet-controladdins>\nSerializing .NET Framework Types<devenv-dotnet-serializing-dotnetframework-types>\nattributes/devenv-method-attributes\nAL Language Extension Configuration<devenv-al-extension-configuration>"}