|
well... this is exactly the same thing, just different syntax (i.e. nice C# syntactic sugar), no?!
when I write:
await WaitForThatEvent()<br />
DoSomethingAfterward()
The compiler generate (roughly.. there is cancellation to consider)
WaitForThatEvent().ContinueWith(t => DoSomethingAfterward());
Isn't that nice?!
modified 27-Sep-16 3:31am.
|
|
|
|
|
How is different to subscribing to an Event and do your stuffs in Event Handlers.
method()
{
WhatEver.MyEvent += (myArgs) => {
}
}
Please help me understand if you are looking for something different which cannot be handled by Events and Delegates (Lambda expressions also simplifies your syntax).
I might have different thought process and not able to understand what exactly you are looking for.
Life is a computer program and everyone is the programmer of his own life.
|
|
|
|
|
How is that different? It is better looking!
turning that basic version
EventHandler ev = null;
ev = (o, e) => {
source.Event -= eh;
DoSomething();
};
source.Event += eh;
into that elegant version
await source.Event.WaitForEvent()
DoSomething();
Can't you appreciate the beautiful simplicity and easier code flow of the second version?!
Added bonus: DoSomething() will try to be in the same thread as the start of the method!!
modified 27-Sep-16 3:22am.
|
|
|
|
|
Yeah, your elegant version look cool while reading. accepted.
But, I personally don't have problem with this too:
source.Event += (o, e) => {
source.Event -= eh;
DoSomething();
};
Your elegant version might get complex when you would have to pass the parameters that DoSomething uses.
oArgType o;
hArgType h;
await source.Event.WaitForEvent(out o, out h);
DoSomething(o, h);
Also, I feel that the generic implementation of "your elegant version" can be achieved by an extension method. I would try to do that in my free time and would post as a reply here.
But nice thought, I appreciate it.
Life is a computer program and everyone is the programmer of his own life.
|
|
|
|
|
On the contrary after some practice with async methods you will see how they simplify all your code, not the opposite. Added bonus code after await is in the initial thread, when possible.
One downside of async method is that they don't take out/ref parameter.
But if you want the event args, here is another version that return the event
public static Task<GpsLocationArgs> WaitForLocationChangedAsync(this IGps gps)
{
var res = new TaskCompletionSource<GpsLocationArgs>();
EventHandler<GpsLocation> eh = null;
eh = (o, e) =>
{
gps.LocationChanged -= eh;
res.TrySetResult(e);
};
gps.LocationChanged += eh;
return res.Task;
}
var loc = await myGps.WaitForLocationChangedAsync();
Console.WriteLine($"({loc.Latitude}, {loc.Longitude})");
sadly, as I complained, it is NOT possible to write a general purpose WaitForEvent() method..
But give it a go, maybe using some LINQ expression magic and some reflection you can come up with a reasonably easy and elegant version....
|
|
|
|
|
I believe you're correct in that you can't do this on the event itself, but you could make something nearly as good (depending on your definition of 'nearly as good').
await source.WaitForEvent<EventArgs>(nameof(source.Event));
Would be possible. Using nameof in VS2015 means you have any issues with refactoring, though it is a bit uglier than your preferred syntax.
Something like the following would implement the method (and returns the EventArgs if you need them). An alternate overload would be needed for non-generic event handler based evevents, but that's not hard.
public static class EventExtensions
{
public static Task<T> WaitForEvent<T>(this object eventSource, string eventName) where T : EventArgs
{
var tcs = new System.Threading.Tasks.TaskCompletionSource<T>();
var eventInfo = eventSource.GetType().GetEvent(eventName);
EventHandler<T> eventHandler = null;
eventHandler = new EventHandler<T>(
(source, e) =>
{
eventInfo.RemoveEventHandler(eventSource, eventHandler);
tcs.TrySetResult(e);
}
);
eventInfo.AddEventHandler(eventSource, eventHandler);
return tcs.Task;
}
}
|
|
|
|
|
Man, I can't upvote this enough!
Awesome, as good as it get, love it!
|
|
|
|
|
Always nice to be appreciated
|
|
|
|
|
By the way, the more I think about this, you can do what you want rather elegantly, using RX. If you view an event as an Observable, then it would be fairly simple to just observe that event and react to it. When I have a couple of free minutes, I'll write you a general purpose example that shows how to do this (and to wait for pretty much any event).
This space for rent
|
|
|
|
|
Your code is eagerly awaited
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
Reactive use a lot of callback as far as I remember...
What I am trying is to make the code seemingly linear and callback free, using await ..
(handy for try/catch, easier to understand the flow as well)
that said.. I would be curious to see what you come back with!
|
|
|
|
|
I understand what you mean. We also have been using such a pattern (your less elegent one) for sometimes.
The event property of an object is essentially a list of handlers for the event, which is designed before the introduction of async/await syntax for asynchronous programming. So it would be "un-natural" to have a method for it at the time.
But yeah, I would also vote for something like that for events to complete the circle (for the introduction of async/await syntax).
|
|
|
|
|
Glad someone understand!
|
|
|
|
|
your prince code will come and rescue you someday <maybe>
|
|
|
|
|
|
I've always been a fan of the Microsoft keyboards. Now it looks like they went more for looks than functionality. I'm currently using a Microsoft 6000[^] keyboard with a slight ergo curve to it. I love it, but they don't make it anymore. Anyone have any suggestions on their fave keyboard? I've only had experience with Logitech and Microsoft and I'm not seeing any of their current products that I like. I'm looking for something that's ergonomic, but not full on split keyboard ergonomic, and reasonably sized. Wireless and a reasonable cost are the only two other criteria.
|
|
|
|
|
Is your current keyboard broken?
In this present crisis, government is not the solution to our problem; government is the problem. ~ Ronald Reagan
|
|
|
|
|
I have some keys that are starting to be less than reliable. I'm expecting failure in the near future.
|
|
|
|
|
How about this keyboard:
[^]
|
|
|
|
|
Bit too ergonomic, lol. I bet once you got used to that thing it would be great.
|
|
|
|
|
Takes some getting use to. I would have liked it if they had kept the control keys in their original locations. It could be worse. I have known people to use the dvorak layout ()[^].
|
|
|
|
|
Unless someone twists your arm, stay away from the Logitech K800. It's rechargeable, which is nice, but it attracts hand oil/dirt like crazy. The keys look pitiful.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I'm using a Maxdata Cherry RS 6000 M.
The back says it's build for Win 95, but it still works on Win 7
It's about 20 years old and they don't make (them like) these anymore.
Good luck finding a keyboard that lasts for 20 years of heavy use!
Not even the letters on the keys have faded!
Unfortunately, they don't make computers with PS2 connectors anymore either, so I'll probably buy a new keyboard with my next computer
|
|
|
|
|
They still make adapters for ps2->usb.
|
|
|
|
|
If you're prepared to roll your own, then PS2 connectors are readily available on motherboards. I just checked a random selection of ASUS, MSI and Gigabyte boards, and all had PS2 ports available.
And of course there's the USB to PS2 adapters. No idea how well they work.
|
|
|
|