Comments (2)
Hi,
Great idea, I will add an event "OnDeviceFound" thrown by DeviceLocator when DeviceLocator.Discover() finds a device.
from yeelightapi.
@roddone @fabiuz7 Sorry, I just came across this ticket. await DeviceLocator.Discover()
is definitely not synchronous. If not used in conjunction with Task.Wait()
this method never executes synchronously. The fact that code seems to "wait" synchronously until completion of await DeviceLocator.Discover()
is the asynchronous behavior. There only seems to be waiting time. In reality execution context is switched to the calling thread. If you are awaiting this method inside a button's event handler the calling thread is the UI thread (main thread). Until the asynchronous method ran to completion, the UI thread continues to execute ( e.g. rendering, event handling) and UI remains responsive. The truth is that events execute synchronously.
An event-driven approach is effective when developing GUI,
This may be right, although you have to realize that it is an old fashioned approach. Asynchronous execution is the modern version of event driven development. sync/await primarily targets UI context or development. The UI thread is the thread you want to keep responsive, without introducing the overhead of multithreading. So async/await is a powerful alternative to event driven development. Async/await is the preferred solution since it enables the asynchronous execution down the complete depth of the otherwise blocking operation. Events don't allow to execute truly asynchronous which is a design choice made by Microsoft. An event is the equivalent to an interrupt on hardware level. it is supposed to interrupt executing routines and therefore executes synchronously. i.e. blocking the executing thread.
There are no asynchronous events. You only cant execute the code of the handler asynchronously. But since event don't return a value by definition, they can't be awaited. They have to be void
. Which means although you can execute code inside a handler using async
you introduce more problems e.g. exceptions are no more thrown into to the context of the caller. This leads to fragile code which allows the application to transition into undefined states.
The decision to introduce an DeviceFound
event based on the claim that an asynchronous method executes synchronously is a bad choice based on lacking understanding. I mean asynchronous and synchronous describes opposite behavior. An asynchronous method can never execute synchronously, it contradicts itself. The method Discover
should rather accept a callback: public async Task<List<Device>> Discover(Action<Device>)
. This would solve the problem that the caller has to "wait" until the complete result was populated but omits the event driven approach in favor of asynchronous driven design.
from yeelightapi.
Related Issues (20)
- [BUG] HOT 16
- [BUG] HOT 1
- Question: is there a way to set a property? HOT 2
- [ENHANCEMENT] use IAsyncEnumerable to provide a more modern way to iterate devices found by DeviceLocator HOT 1
- Unable to detect Mi LED Smart Bulb[BUG] HOT 2
- Call to DeviceLocator.Discover() in a Xamarin Android project does not return any devices HOT 9
- [BUG] AdjustBright doesn't allow negative values
- [QUESTION] Is it possible to connect to the Yeelight device and send some commands from the device/application that is NOT in the same LAN? HOT 2
- [BUG] LED Ceiling Light brightness always returns 1 in moonlight mode HOT 2
- [SOLVED] Connect issue HOT 1
- [BUG] DeviceLocator throwing InvalidOperationException HOT 13
- Q: Support for music mode? HOT 11
- [Feature-request][Question] HOT 1
- [BUG] DeviceGroup.StartMusicMode() won't work for multiple devices HOT 6
- How it work? HOT 2
- Only works once HOT 1
- DeviceLocator.UseAllAvailableMulticastAddresses not returning all devices in v1.10.2. HOT 9
- [BUG] HOT 4
- [QUESTION] Should I create new tcp connection on every http request or persist an exist one ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yeelightapi.