|
About that, when are you going to go back in time properly?
Not this brexit nonsense, I bet the kids of today can't wait to start counting their money on both their hands and their feet.
|
|
|
|
|
Jörgen Andersson wrote: counting their money on both their hands and their feet.
Knowing today's kids, you don't want to go back to using guineas.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Not enough of them having six toes on one foot?
|
|
|
|
|
Canada hasn't had real money since 1967, the last full year of silver coinage. If you do the conversion between Imperial and metric, a dime bought a liter of gas then. It still does, though you'd have to take it to a coin shop first.
The last year for the US was 1964, but I don't know the last year for the UK.
|
|
|
|
|
I'm wrapping a bluetooth library because i have to. So much of it is asynchronous, but it uses an event model for the asynchronicity which if you think about it makes everything more difficult.
Consider the simple act of terminating a connection. It can take some time over the bluetooth protocol to do so gracefully.
The library exposes a (weirdly named but okay) "OnDisconnect" event from its client object. You must hook that event to know when the disconnection is complete. (This is especially critical for methods like Connect that aren't usually fire and forget but disconnect is simpler so i'm using it here)
It means you have to hook an event to find out when you can keep going, and you often have to unhook your handler from the event when you're done because next time you call disconnect or connect or whatever, you may need to do a different thing this time when it completes!
Callbacks would have been easier.
Worse, you can't pass any kind of state argument to Connect or Disconnect, or any other async method, which means in the real world, you pretty much have to hoist arguments and vars from your surrounding code, which means handling your events using anonymous method lambdas, which means it's very difficult to unhook your anonymous handler from the event when you're done.
This kind of thing above - exposing something that should be a callback as an event, and then not allowing a state parameter is an example of how not to do it.
In order to make stable code, I've been wrapping the entire library with something that uses exceptions instead of int error result values (correcting another sin) and then wrapping the event model with microsoft's awaitable TAP pattern (ie: using the familiar task framework)
It's a pain in the elephant because of the mess above.
This is what it looks like, even for the simplest thing. Don't do it, kids. If you're going to make code that exposes asynchronicity plan it for real world scenarios.
public int Disconnect(int millisecondsTimeout = Timeout.Infinite)
{
if (_gattClient.State != wclClientState.csConnecting &&
_gattClient.State!=wclClientState.csConnected)
throw new InvalidOperationException("The client is not connected");
int result = 0;
using (var ev = new AutoResetEvent(false))
{
wclClientConnectionDisconnectEvent cde = (object sender, int reason) =>
{
result = reason;
ev.Set();
};
_gattClient.OnDisconnect += cde;
BleException.Check(_gattClient.Disconnect());
if (!ev.WaitOne(millisecondsTimeout))
throw new TimeoutException("The disconnect operation timed out");
_gattClient.OnDisconnect -= cde;
}
return result;
}
wclClientConnectionDisconnectEvent _cdeHandler;
public Task<int> DisconnectAsync()
{
if (_gattClient.State != wclClientState.csConnecting &&
_gattClient.State != wclClientState.csConnected)
throw new InvalidOperationException("The client is not connected");
var tcs = new TaskCompletionSource<int>();
System.Diagnostics.Debug.Assert(null == _cdeHandler,"The async disconnect was somehow already in progress");
_cdeHandler = (object sender, int reason) =>
{
tcs.SetResult(reason);
_gattClient.OnDisconnect -= _cdeHandler;
_cdeHandler = null;
};
_gattClient.OnDisconnect += _cdeHandler;
BleException.Check(_gattClient.Disconnect());
return tcs.Task;
}
Real programmers use butterflies
|
|
|
|
|
It has been a long time I had to work with multiple event chains but....
Last I did, I turn them into IObservable and that help me express concisely what would have been messy otherwise...
Have a look at Rx.NET and learn it a bit!
GitHub - dotnet/reactive: The Reactive Extensions for .NET
I always despised working event and event handler and state variable, it's very spaghetti prone
But by turning events into Observables I can describe what I want and my conditions in very expressive and simple fashion!
|
|
|
|
|
I prefer, where possible to turn them into TAP patterns (async/await). That's what I'm doing with that code above. It's ugly because it hides ugly details behind a nice clean TAPpy API
Real programmers use butterflies
|
|
|
|
|
MM.,... just read a bit about...
it looks like it's about turning 1 event into 1 async method...
Rx.NET and Observable it's about something like LINQ to query multiple events, in any order.. not just one!
|
|
|
|
|
TL;DR, but I'm sure there's a braceless single-line if-statement in there somewhere, so prepare to be pitchforked just in case
|
|
|
|
|
You've got it all wrong. Mad scientists are pitchforked; witches are burnt!
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Next post:
honey the codewitch wrote: I'm wrapping creating a bluetooth library because i have to.
Sorry, but I had to.
|
|
|
|
|
*cries*
Real programmers use butterflies
|
|
|
|
|
I expected a witty answer, now I feel bad.
|
|
|
|
|
I'm over it. I just decided to continue writing the wrapper around this mess.
Real programmers use butterflies
|
|
|
|
|
If "The disconnect operation timed out", does that mean you are still connected?
I’ve given up trying to be calm. However, I am open to feeling slightly less agitated.
|
|
|
|
|
I wish. More likely it means your connection is in an unknown state waiting for an APC message to be dispatched that got lost somewhere.
Real programmers use butterflies
|
|
|
|
|
Seems everyone got lucky considering the traffic here compared to yesterday.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
We expecting a drop of 14C between Wednesday and Thursday - at daytime...
It will be just a hair above 0 and will go under at night...
I bought 40 litres of kerosene and two new mobile heater...
Thinking of a way to cover the flowers that were deceived into spring in the last two weeks (near 20C at day) and dared to flower...
"The only place where Success comes before Work is in the dictionary." Vidal Sassoon, 1928 - 2012
|
|
|
|
|
Pussy ... We're currently sitting at -25C with a wind chill of -37C.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Isn't that "Summer" in Canadia?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Summer is the "bad skiing" period ...
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Crikey we got down to 24c last night here in the sunshine (no that is not a euphemism) state of Queensland.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Things are looking up ... it was -32C (without the windchill) the other day.
Spring started February 3 (solar terms calendar).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
-10 here for the last two days an most probably for the next two days also *ice*
modified 27-Mar-21 21:01pm.
|
|
|
|
|
be careful not setting your place on fire.
I'd rather be phishing!
|
|
|
|