Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a delegate with a null parameter?

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!

like image 420
Rodney Burton Avatar asked Jun 01 '10 21:06

Rodney Burton


People also ask

How do you pass a null as a parameter?

You cannot pass the null value as a parameter to a Java scalar type method; Java scalar types are always non-nullable. However, Java object types can accept null values. The values of both variable <@I> and variable <@A> are null, since values have not been assigned to them.

How do I invoke a delegate?

Create the delegate and matching procedures Create a delegate named MySubDelegate . Declare a class that contains a method with the same signature as the delegate. Define a method that creates an instance of the delegate and invokes the method associated with the delegate by calling the built-in Invoke method.

Can we pass delegate as parameter?

Because the instantiated delegate is an object, it can be passed as an argument, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time.

How do you assign a delegate to a function?

Delegates can be invoke like a normal function or Invoke() method. Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-=" operator. It is called multicast delegate. If a multicast delegate returns a value then it returns the value from the last assigned target method.


1 Answers

Just for the benefit of others, you can pass null arguments to delegates (if the type allows it? Clarification needed here). In your case, IAsyncResult will allow it.

As for the debugging, the exception occurs on Invoke because you are debugging on a given Thread A, the exception occurs on Thread B. You can breakpoint multiple threads. Breakpoint the Thread B code and you will see the exception closer to or on the source.

Notice though that your debugger will jump around if multiple threads are running code at the same time. Debugging in multiple threads is always at least a little tricky, but satisfying when you solve the problems.

You could also further improve your answer code to check the null before it checks the InvokeRequired, as this is thread-independent to your logic (your code checks it just prior to use, after Invoking). This will save pushing the Invoke onto the message pump (assuming WinForms).

like image 54
Adam Houldsworth Avatar answered Sep 22 '22 16:09

Adam Houldsworth