سؤال

I am porting some C# code (classes mostly) to C. I have analysed the code and the port is definitely possible. One thing I am not sure of though, is how to handle (i.e. implement) raising events in C.

I am in a Linux environment, and I expect the binary to run on Linux only. I heard that GLib supports events, but don't know much about this - any help would be appreciated.

A hello world example of raising an event and handling it in C would be very useful.

هل كانت مفيدة؟

المحلول

The closes thing to C# events implemented by GLib are the GObject signals.

Use g_signal_connect to connect your callback to an existing signal, and g_signal_emit to emit a registered signal. See the documentation for details.

As you are coming from a C# background, you might also consider using Vala, a programming language built around the GObject object system that compiles to C and has a feel similar to that of C# or Java.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top