Tech Off Thread

2 posts

Defining a component's default properties

Back to Forum: Tech Off
  • User profile image
    Andrew Davey

    Hello fellow 9ers,

    I am creating a component that exposes the following property to enable events to be raised on the host form, in that form's main thread.

    SynchronizingObject() As System.ComponentModel.ISynchronizeInvoke

    Everthing works fine, except I cannot work out the attribute needed to make the designer automatically set that property to "Me" (i.e. the form) when added to a form. The Timer component does this so it is possible.

    Any ideas? Thanks,

    Andrew

  • User profile image
    richard_dee​ming

    Looking at the source of the System.Timers.Timer component, the property looks like:

    public ISynchronizeInvoke SynchronizingObject
    {
        get
        {
            IDesignerHost host1;
            object obj1;
            if ((this.synchronizingObject == null) && base.DesignMode)
            {
                host1 = ((IDesignerHost) this.GetService(typeof(IDesignerHost)));
                if (host1 != null)
                {
                    obj1 = host1.RootComponent;
                    if ((obj1 != null) && ((obj1 as ISynchronizeInvoke) != null))
                    {
                        this.synchronizingObject = ((ISynchronizeInvoke) obj1);
                    }
                }
            }
            return this.synchronizingObject;
        }
        set
        {
            this.synchronizingObject = value;
        }
    }


    An alternative is to look at the target of the event handlers, to see if they support ISynchronizeInvoke:

    public static void Fire(Delegate del, bool async, params object[] args)
    {
        if (null == del) return;
       
        Delegate[] list = del.GetInvocationList();
        if (null == list || 0 == list.Length) return;
       
        for(int i=0; i<list.Length; i++)
        {
            Delegate d = list[i];
            ISynchronizeInvoke isi = del.Target as ISynchronizeInvoke;
            if (null == isi || !isi.InvokeRequired)
                d.DynamicInvoke(args);
            else
                isi.Invoke(d, args);
        }
    }

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.