Troubles with Me.Invoke

mariano_donati

Active member
Joined
Nov 30, 2005
Messages
41
Programming Experience
Beginner
Hi, I got some really weird exception (not the exception by itself, but the context) other day. When my client application receives a message, it raises an event. Due to this event is generated in a background thread, I need to handle it in main form with delegates. This is what I do:

Private Sub Client_MessageReceived(....) Handles Client.MessageReceived
Dim Del As New DShowChatForm(AddressOf ShowChatForm)
Me.Invoke(Del, New Object(){'Here I pass all the arguments})
End Sub

Delegate Sub DShowChatForm('Here I pass the same arguments)

Private Sub ShowChatForm('same thing here)
'This is the sub wich actualy handles the event
End Sub

The thing is when I tried to execute Me.Invoke, It says "NullReferenceException". I've got understood that I exception only appears when you're trying to use the properties and methods of an object without making (or receiving) an explicit instance of it.
The weird thing is that I've done, since I started with this kinds of applications, like 3 or 4, and each of these applications has the same structure to handles the MEssageReceived event, and none of them gave me some kind of trouble. So, I really don't know what's wrong with it.
Is it possible that exception appears because of arguments that I pass in the Me.Invoke method?
Hope someone can help me out with this.
Thank you all very much in advance.
Regards!.
 
I could sove it. It wasn't the arguments that I was passing to delegate, but a null reference in the AddressOf sub. I had a line like this:

Dim ActiveClient As OnlineClient = HOnlineClients(IdFromMessage)

OnlineClient is a structure and HOnlineClients is a hashtable wich keeps Id of online clients as key and Onlineclient structure, with client info, as value. So, the trouble was that I wasn't adding any client to hashtable when he connected and that's why when I tried to get the client info it returned a null reference, cuz it obviously couldn't find the key.

I don't know if that could help somebody, but I'm member of other forums in the net, and they keep a rule that when you solve your trouble, then you can post how you solve it and what was really the error that was blowing your mind off.

Regards!.
 
Mariano said:
I don't know if that could help somebody, but I'm member of other forums in the net, and they keep a rule that when you solve your trouble, then you can post how you solve it and what was really the error that was blowing your mind off.

Very, very good rule and we are practicing the same if i may to say so.
Thanks a lot Mariano, we really appreciate it :)
Regards ;)
 
Back
Top