Delegate to an instance method cannot have null 'this'.

I'm trying to convert my multi-threaded application over to delegates so that it is thread safe. I'm getting the error "Delegate to an instance method cannot have null 'this'".

This is on the UI Thread,

   Public Sub UpdateStatusLabelMethod(ByVal strMessage As String())

 Dim intLabel As Integer = strMessage(0)
        Dim strStatusMessage = strMessage(1)

 TabControl1.TabPages(2).Text = strStatusMessage
   End Sub

The text is updated successfully.

This is the code within the worker thread class,

Delegate Sub UpdateStatusLabelHandler(ByVal strMessage As String())

Private Sub GetAnswer
     Dim argsStatus As String() = {WorkerCount, "[" & intTried & "]." & strDots}

End Sub

   Public Sub UpdateStatusLabel(ByVal strMessage As String())

        Dim f As Form1 = My.Forms.Form1.ActiveForm
        f.Invoke(New UpdateStatusLabelHandler(AddressOf f.UpdateStatusLabelMethod), _
                 New Object() {strMessage})
    End Sub

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Göran AnderssonCommented:
You failed to get a reference to the form. The reference f is Nothing, and you can't use a method in an object that doesn't exist.

Check what you get from the ActiveForm property before you try to use it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JPERKS1985Author Commented:
does it mean that sometimes it can't find it, but other times it can?
Göran AnderssonCommented:

From the documentation of the ActiveForm property:

"Property value:
A Form that represents the currently active form, or a null reference (Nothing in Visual Basic) if there is no active form."

Also, you are implicitly casting the return value of the property from the class Form to the class Form1. If the active form is of a different class that will not work. You should set Option Strict On to avoid unintentional casting in your code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.