Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I've been trying to learn network programming so I downloaded the chat example that Microsoft has on their website and have been making some modifications to it. I added in private messaging and some other features. There a few bugs with it, but there is one problem that can't seem to fix. This is the excpetion that is being thrown.


************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
  at System.Windows.Forms.RichTextBox.EditStreamProc(IntPtr dwCookie, IntPtr buf, Int32 cb, Int32& transferred)
  at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
  at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
  at System.Windows.Forms.Control.DefWndProc(Message& m)
  at System.Windows.Forms.Control.WndProc(Message& m)
  at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
  at System.Windows.Forms.RichTextBox.WndProc(Message& m)
  at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
  at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

 

Now Usually I set a break point and step through my code to find out whats going on, but the program works fine when I'm debugging through Visual Studio. The error only occurs when I run the 'exe' located in the bin folder. The error occurs when a client trys to connect to the server. The confusing part is that sometimes it works and sometimes it doesn't. Theres a 50/50 chance the program will work fine, or crash. To make a long story, I cant fix the error, because I can't duplicate the error while Im debugging in visual studio. I've attached the source code for the server and client. I would MUCH appreciate some help on this. Thanks in advance.

ServerClientChat.zip

Whatever thy hand findest to do, do it with all thy heart - Jesus
  • Administrators
Posted

Not had chance to have a proper look but you have a method

Private Sub DisplayText(ByVal Str As String)
       Try
           txtInfo.AppendText(Str)
           txtInfo.Focus()
           txtInfo.SelectionStart = txtInfo.Text.Length
           txtInfo.ScrollToCaret()
           txtSend.Focus()
       Catch
           MessageBox.Show("Server Error: " & Err.Number & vbCrLf & Err.Description)
       End Try
   End Sub

which is updating the main form from a thread other than the main UI thread - this is generally a 'bad idea'. You should use the .Invoke method of the form to ensure it is being called on the primary thread. If you search these forums I'm fairly sure the topic has cropped up before.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

You are an absolute genius Damp. I would have never known about that until you mentioned it. I'm still in the learning process. :p Anyways, I looked into

what you said, and I'm pretty sure that was the problem. Instead of directly calling the method, I did this

 

  'I Put This In The Form Somewhere
  Private Delegate Sub DisplayDelegate(ByVal Data As String)

 

And Inside Of My DoListen Method, I Put This In Place Of The DisplayText Method

 


     Dim Dlg As New DisplayDelegate(AddressOf DisplayText)
     Dim Str() As Object = {"New Connection Found" & vbCrLf}              
     Me.Invoke(Dlg, Str)

 

 

Like I said before, I'm still learning and this concept was totally new to me, as is sockets and listeners and such. But the application seems to run fine now. I've ran it close to fifty times and no crashes and usually, I'd get a crash every other time I launched it, so I'm assuming the problem is fixed.

Thanks again Damp, Much appreciation on this end :D

 

-=Simcoder=-

Whatever thy hand findest to do, do it with all thy heart - Jesus

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...