Tech Off Thread

7 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

InvokeRequired, InvokeRequired, InvokeRequired...

Back to Forum: Tech Off
  • User profile image
    esoteric

    I'm building a small application with Windows Forms and see this pattern constantly -

            public void Transfer(IList<Hash> items)
            {
                if (InvokeRequired)
                {
                    Invoke(new ProcessDelegate<IList<Hash>>(Transfer), new object[] { items });
                }
                else
                {
                    foreach (var e in items)
                    {
                        hashBindingSource.Add(e);
                        Application.DoEvents();
                    }
                }
            }

    Is there a better way to do this? I imagine having an attribute on the method doing code-injection to create this pattern automatically - including creating any necessary delegate types.

    Oh thread-safe scenegraph - where art thou.

  • User profile image
    esoteric

    To preempt any answers, I looked at google and found this


  • User profile image
    dcuccia

    After only finding AOP approaches to this on the google, I suggested an extension method solution recently on C9. What do you think of that approach? 

  • User profile image
    turrican

    esoteric said:
    To preempt any answers, I looked at google and found this


    You searched the Internet using Google, found and looked at "ISerializable - Roy Osherove's Blog"... *correcting*

    Edit : What I mean is, Google != Internet

    ps.  Yes, I know... Off topic! Tongue Out

  • User profile image
    littleguru

    I don't like the current way either and therefore implemented a small class to help me out: http://www.liensberger.it/web/blog/?p=90 - I hope that helps you guys too.

    The approach could be modified to use extension methods, but I didn't do that to keep it compilable for the C# 2.0 compiler.

  • User profile image
    Dexter

    littleguru said:

    I don't like the current way either and therefore implemented a small class to help me out: http://www.liensberger.it/web/blog/?p=90 - I hope that helps you guys too.

    The approach could be modified to use extension methods, but I didn't do that to keep it compilable for the C# 2.0 compiler.

    Hmm... why would you bother to do that when Invoke/InvokeRequired are bugged in all kinds of ways? What's wrong with using BackgroundWorker or SynchronizationContext?

  • User profile image
    esoteric

    dcuccia said:
    After only finding AOP approaches to this on the google, I suggested an extension method solution recently on C9. What do you think of that approach? 
    Not bad, but still not quite as transparent as one would like.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.