17. Advanced programmingThe preceding chapters have introduced, in each available language, the basic programmingfunctions which can be used with your Yocto-GPS module. This chapter presents in a more genericmanner a more advanced use of your module. Examples are provided in the language which is themost popular among Yoctopuce customers, that is C#. Nevertheless, you can find completeexamples illustrating the concepts presented here in the programming libraries of each language.To remain as concise as possible, examples provided in this chapter do not perform any errorhandling. Do not copy them "as is" in a production application.17.1. Event programmingThe methods to manage Yoctopuce modules which we presented to you in preceding chapters werepolling functions, consisting in permanently asking the API if something had changed. While easy tounderstand, this programming technique is not the most efficient, nor the most reactive. Therefore,the Yoctopuce programming API also provides an event programming model. This techniqueconsists in asking the API to signal by itself the important changes as soon as they are detected.Each time a key parameter is modified, the API calls a callback function which you have defined inadvance.Detecting module arrival and departureHot-plug management is important when you work with USB modules because, sooner or later, youwill have to connect or disconnect a module when your application is running. The API is designed tomanage module unexpected arrival or departure in a transparent way. But your application must takethis into account if it wants to avoid pretending to use a disconnected module.Event programming is particularly useful to detect module connection/disconnection. Indeed, it issimpler to be told of new connections rather than to have to permanently list the connected modulesto deduce which ones just arrived and which ones left. To be warned as soon as a module isconnected, you need three pieces of code.The callbackThe callback is the function which is called each time a new Yoctopuce module is connected. It takesas parameter the relevant module.static void deviceArrival(YModule m){Console.WriteLine("New module : " + m.get_serialNumber());}www.yoctopuce.com 117