Link to home
Start Free TrialLog in
Avatar of zattz
zattz

asked on

Pass Method in Parameter

Hi,

I have never used delegates before and I am totally lost in the documentation.

Here is my code:

public static void DownloadAssetGroups()
        {
            IBaseRepository repository = (activity.Application as AssetManagerApplication).AssetGroupRepository;            
            PerformReceiveOnRepository(repository);
        }

        private static void PerformReceiveOnRepository(IBaseRepository repository)
        {
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            ReceiveStatusResult statusResult = new ReceiveStatusResult();
            ThreadPool.QueueUserWorkItem((s) => ReceiveClass.Receive(manualResetEvent, activity, repository, statusResult));

            ThreadPool.QueueUserWorkItem((s) =>
            {
                manualResetEvent.WaitOne();
                if (statusResult.Error) showError(statusResult);
                else
                {
                    resultToastMessage += (activity.Application as AssetManagerApplication).AssetGroupRepository.GetObjectTypeClassDisplayNamePlural() + " Downloaded " + statusResult.DownloadCount.ToString() + " New " + statusResult.NewCount.ToString() + " Modified " + statusResult.ModifiedCount.ToString() + "" + System.Environment.NewLine + System.Environment.NewLine;
                    //nextMethod());  
                }
            });
        }

Open in new window


Basically I call DownloadAssetGroups() which calls PerformReceiveOnRepository.

However, at the end of the repository I want to call another method, on the line I've commented on that says // nextMethod(), I want to pass the nextMethod into the PerformReceiveOnRepository method

Can anybody help?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zattz
zattz

ASKER

I'm still lost as how I would apply that to my above code
What's the signature for your target method?  Start by making a delegate that has that same signature.  Then add a parameter to your existing PerformReceiveOnRepository() method so it can receive another parameter of your new delegate type (along with any other pertinent parameters).  Finally, at the end of PerformReceiveOnRepository(), simply execute your delegate (passing parameters as necessary).
Avatar of zattz

ASKER

I got it to work by trial and error. I still need to take the time to understand delegates, but I was under a tight deadline. Thank you very much