Avatar of vivekj2004
vivekj2004 asked on

C# InvokeRequired and MethodInvoker()

Problem is that I cannot use the commented part of the code because MethodInvoker does not take any arguments, but I want to call the method which has aguments (only 1 actually). How can I do this. Can somebody answer this quickly?

 public void DisplayProductFamilies(List<RetentionProductFamilyDisplayDTO> families)
        {
            if (this.retentionControl.InvokeRequired)
            {
                MessageBox.Show(" Display Product Families not invoked by the retentionControl UI control");
            //    MethodInvoker method = new MethodInvoker(this.DisplayProductFamilies);
            //    this.retentionControl.Invoke(method);
            //    return;
            }
           
            NavBarControl navBar = retentionControl.NavBarProductFamilies;
            if (navBar != null)
            {
                navBar.Groups.Clear();
            }
            if ((families != null) && (families.Count != 0))
            {
                foreach (RetentionProductFamilyDisplayDTO retentionFamily in families)
                {
                    retentionControl.DisplayProductFamilies(retentionFamily);
                }
            }
            else
            {
                //   Hide(true);
                retentionControl.DisplayProductFamilies(null);
            }
        } 

Open in new window

C#

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
kaufmed

Perhaps?
this.retentionControl.Invoke((Action<List<RetentionProductFamilyDisplayDTO>>)this.DisplayProductFamilies, families);

Open in new window

ASKER CERTIFIED SOLUTION
ajb2222

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
vivekj2004

well kaufmed! The code compiled.... but may I know what is this line doing. I mean... If Invoke required this method should be called again. How is it being called again using this line of code?
kaufmed

I don't know all the ins and outs of Invoke, but as I understand it, when you call a method that interacts with a control and that method is being executed on a thread other than the one that created the control, an Invoke is required. For the code above, when this "secondary" thread enters DisplayProductFamilies(), the InvokeRequired returns true. This subsequently causes the Invoke to be called. This is the part I am not sure what goes on internally. What I don know is that on the 2nd call to DisplayProductFamilies(), since Invoke() was called, InvokeRequired will return false and therefore the subsequnt code after the "if" is executed.
Your help has saved me hundreds of hours of internet surfing.
fblack61
kaufmed

So glad I took the time to post an explanation. Glad to help  :)