1

For an application that I'm working on I use SpotifyLocalAPI, and I want to use the events that the API has. But, as someone who is into C# for a couple of months now, I'm not sure where to start. There is another project based on this API that uses the events, but it's in WPF, and that makes it a different deal if I understand my googeling correctly. This means that for a WinForms I have to do things a bit differently, but I can't seem to figure out how.

The documentation of the API states that You can set a SynchronizingObject, then the events will be called on the specific context. When I look at how the WPF project did this, it has a function (found here) to do some magic, and poof, it works.

If I understand this answer correctly the SynchronizingObject is a property of the ISynchronizeInvoke interface, which "provides synchronous and asynchronous communication between objects about the occurrence of an event.".

Okay, so far so good. I think I understand the basic working of the interface, but how am I supposed to work with it? How do I convince the application that it should react to the event? How should I define the _spotify.SynchronizingObject? (Which is the main problem for me right now)

Community
  • 1
  • 1
MagicLegend
  • 328
  • 1
  • 5
  • 22

1 Answers1

1

You can set the SynchronizationObject to be any UI element that implements IShynchronizeInvoke (Form, UserControl etc). Check out the example Winforms app here. Note that this is optional, and in that example app, they have chosen to use Invoke() explicitly in the event handlers. The important thing to remember is that if you want to update the UI, then the code to do so must be run on the UI thread. Some more details on this here.

Matt Cole
  • 2,491
  • 17
  • 21
  • Thank you for all the information :) I totally overlooked the example winforms app that the API provided :c It works like a charm now! Back to more things to do and stuff to fix :p – MagicLegend Jan 06 '17 at 17:06
  • 1
    @MagicLegend As the author of the library, I strongly suggest invoking on the UI-Thread inside the event-handlers. The problem: When setting the `SynchronizingObject`, the whole timer-callback (which also makes the web-request) will be called on the specific thread. In most of the cases, this is unwanted behavior. It will be removed once .net core support is finished. – Jonas Dellinger Jan 10 '17 at 18:26