How to invoke a delegate with a null parameter?

Posted by Rodney Burton on Stack Overflow See other posts from Stack Overflow or by Rodney Burton
Published on 2010-06-01T21:16:09Z Indexed on 2010/06/01 21:33 UTC
Read the original article Hit count: 245

I get a null exception if I try to pass a null parameter to a delegate during an invoke. Here's what the code looks like:

        public void RequestPhoto()
        {
            WCF.Service.BeginGetUserPhoto(Contact.UserID,
                new AsyncCallback(RequestPhotoCB), null);
        }

        public void RequestPhotoCB(IAsyncResult result)
        {
            var photo = WCF.Service.EndGetUserPhoto(result);
            UpdatePhoto(photo);
        }

        public delegate void UpdatePhotoDelegate(Binary photo);
        public void UpdatePhoto(Binary photo)
        {
            if (InvokeRequired)
            {
                var d = new UpdatePhotoDelegate(UpdatePhoto);
                Invoke(d, new object[] { photo });
            }
            else
            {
                var ms = new MemoryStream(photo.ToArray());
                var bmp = new Bitmap(ms);
                pbPhoto.BackgroundImage = bmp;
            }
        }

The problem is with the line:

Invoke(d, new object[] { photo });

If the variable "photo" is null. What is the correct way to pass a null parameter during an invoke? Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about delegates