Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. C# syntax I wish for

C# syntax I wish for

Scheduled Pinned Locked Moved The Lounge
csharpcomlounge
24 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Yortw

    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(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 WaitForEvent(this object eventSource, string eventName) where T : EventArgs
    {
    var tcs = new System.Threading.Tasks.TaskCompletionSource();
    var eventInfo = eventSource.GetType().GetEvent(eventName);

    		EventHandler eventHandler = null;
    		eventHandler = new EventHandler(
    			(source, e) =>
    			{
    				eventInfo.RemoveEventHandler(eventSource, eventHandler);
    				tcs.TrySetResult(e);
    			}
    		);
    		eventInfo.AddEventHandler(eventSource, eventHandler);
    
    		return tcs.Task;
    	}
    }
    
    S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #21

    Man, I can't upvote this enough! Awesome, as good as it get, love it! :-D

    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

    Y 1 Reply Last reply
    0
    • S Super Lloyd

      Man, I can't upvote this enough! Awesome, as good as it get, love it! :-D

      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

      Y Offline
      Y Offline
      Yortw
      wrote on last edited by
      #22

      Always nice to be appreciated :)

      1 Reply Last reply
      0
      • S Super Lloyd

        Not sure where to post that, (there might some sort of MSDN link somewhere) for now I just post there. "Sometimes" one has to wait for an event to be triggered (once) and then do something, I'd like to write a WaitForEvent() method. Unfortunately, due to the particular of event syntax, I can't and I have to write one wait for *that* event method for every single event I am interested in!... :(( Mmm... case for a code sinppet me just think! :omg: :-\ :rolleyes: In any case here is the syntax for a specific event:

        Task WaitForThatEventAsync(Foo source)
        {
        var res = new TaskCompletionSource<bool>();
        EventHandler eh = null;
        eh = (o, e) =>
        {
        source.ThatEvent -= eh;
        res.TrySetResult(true);
        };
        source.ThatEvent += eh;
        return res.Task;
        }

        EDIT My explanation seem confusing.. another explanation is that I would like to be able to write a "general purpose" WaitForEvent() extension method that transform this cluncky code

        EventHandler ev = null;
        ev = (o, e) => {
        source.Event -= eh;
        DoSomething();
        };
        source.Event += eh;

        into this elegant code

        await source.Event.WaitForEvent()
        DoSomething();

        where both do the exact same thing! Sadly this is not possible right now... I have a to write a new WaitForThatEvent() method for each event I am interested in... :(( but it would be cool. Would make ReactiveC# better looking too.

        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

        U Offline
        U Offline
        User 10495434
        wrote on last edited by
        #23

        your prince code will come and rescue you someday ;P

        S 1 Reply Last reply
        0
        • U User 10495434

          your prince code will come and rescue you someday ;P

          S Offline
          S Offline
          Super Lloyd
          wrote on last edited by
          #24

          It already had!! :omg: :-\ The Lounge[^]

          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups