Getting Events in Windows Forms app

I’m trying to follow the Win SDK documentation, but its too sparse for me to follow (my c#-fu is weak).
I have my windows Form app successfully logging in, retrieving my device list and their published variables, and I can send data to my devices - but i can’t figure out how to subscribe to the event stream.

If I copy the example code :

private void onEvent(object sender, ParticleEventResponse particeEvent)
  Debug.WriteLine($"Got Event {particeEvent.Name} with data {particeEvent.Data}");

Guid eventListenerID = ParticleCloud.SharedCloud.SubscribeToAllEventsWithPrefixAsync(onEvent, "temp");

Visual Studio complains about the last line:

Error	CS0236	A field initializer cannot reference the non-static field, method, or property 'Form1.onEvent(object, ParticleEventResponse)'	WGcontrol	C:\Users\paul\documents\visual studio 2015\Projects\WGcontrol\WGcontrol\Form1.cs

What am I doing wrong? I’m putting the code in Form.cs - is that the right place?

Also what is this?

ParticleCloud.SharedCloud.SynchronizationContext = System.Threading.SynchronizationContext.Current;

Do I need it? Where do I put it?

How about declaring onEvent as static void?

I did try that, but then it complains about

Guid eventListenerID =  ParticleCloud.SharedCloud.SubscribeToAllEventsWithPrefixAsync(onEvent, "temp");

with the error

Error CS0029 Cannot implicitly convert type ‘System.Threading.Tasks.Task<System.Guid>’ to ‘System.Guid’ WGcontrol C:\Users\paul\documents\visual studio 2015\Projects\WGcontrol\WGcontrol\Form1.cs

I’m not very familiar with the C# SDK but if you are calling .....Async() functions, would you not use the await keyword with it?

I’ve needed to add that to other parts of the example code, but it just creates more errors in this bit. Does not seem to be applicable in this instance.

Ok, fresh cup of coffee, and i’ve made it work.

Guid eventListenerID = await ParticleCloud.SharedCloud.SubscribeToAllEventsWithPrefixAsync(onEvent, "temp");

does need the ‘await’ operator, and needs to be located inside a method labelled with ‘async’.

Thanks for the brain prods … :smile:

1 Like