tags:

views:

98

answers:

2

I have the following line of code:

    public bool dcpl_radar()
    {
        if (radar == null)
            return false;
        else
        {
            if (radar != null)
            {
                if (radar.InvokeRequired)
                    radar.BeginInvoke(new MethodInvoker(delegate() 
                                                { 
                                                    radar.Visible = false; 
                                                }));
                else
                    this.radar.Visible = false;

                radar = null;
            }
            return true;
        }//end of else statement
    }

but VStudio keeps throwing an error on the invoke line. I've checked the Debugger and if (radar == null) is true, yet VStudio is trying to evaluate a part of the code it shouldn't be in. Can someone explain why it's doing this please?

+4  A: 

Wait a minute... I think we have a race condition. Lets say you BeginInvoke, almost immediately you set radar = null. There really is no telling when your anonymous delegate will be executed.

I would imagine this should solve your issue.

public bool dcpl_radar()
{
    if (radar != null)
    {
        if (radar.InvokeRequired)
        {
            radar.BeginInvoke(new MethodInvoker(HideRadar));
        }
        else
        {
            HideRadar();
        }

        return true;
    }

    return false;
}

private void HideRadar()
{
    this.radar.Visible = false;
    this.radar = null;
}
ChaosPandion
That won't work as the block of code in the If statement is still being executed even though it evaluates to false.
Dark Star1
+1 for Race condition. MethodInvoker will most likely execute after radar = null is called, so `radar.Visible` will throw an exception. Alternatively, multiple threads could cause the same problem.
Nader Shirazie
Also judging by this code I can make a very educated guess that radar is a field with no locks.
ChaosPandion
This method belongs to a class object and the object is locked during modification. also I'm only using one thread so far.. but thanks for reminding me I'm using anonymous delegate. Damn I should've clicked on that.
Dark Star1
Simple solution (if radar must be set to null) would be to set it to null in the "else" condition and in the anonymous method body after radar.Visible is modified.
Nader Shirazie
+1  A: 

What is happening:

The anonymous delegate is being called after you set the radar to null.

How to fix it

public bool dcpl_radar()
{
    if (radar == null)
        return false;
    else
    {
        if (radar != null)
        {
            if (radar.InvokeRequired)
                radar.BeginInvoke(new MethodInvoker(delegate() 
                                            { 
                                                radar.Visible = false; 
                                                radar = null;
                                            }));
            else {
                this.radar.Visible = false;
                radar = null;
            }


        }
        return true;
    }//end of else statement
}

(Note where I've moved your 'null' assignments).

Though I am a bit worried about the point of setting a variable to null, it's generally a sign of a bad design.

Noon Silk
My guess is that he is not a fan of methods :)
ChaosPandion
Well..... I am a fan of shortcuts to a fault
Dark Star1