EDN Admin
Well-known member
I have a problem with the code I will be posting down below, when I use the logout button & then I proceed to re-join & the server gets taken down the client crashes/closes, debug mode shows errors in general as the debug session closes out but it
doesnt go to any lines of code or anything for me to find where the problem is happening at.
Now I know this isnt a server issue for sure, no bugs show up in the servers coding program at all & it shouldnt take the servers help for a client to disconnect properly between my app being closed & them using the logout button. It only
bugs when the server goes down & only after having used the logout button since youve opened the application...
Here is the code...and the bug is .... <span style="font-family:Consolas; font-size:xx-small
<span style="font-family:Consolas; font-size:xx-small
A first chance exception of type System.InvalidOperationException occurred in System.Windows.Forms.dll
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.Net.Sockets
<span style="color:Blue; Imports System.Text
<span style="color:Blue; Public <span style="color:Blue; Class Form2
<span style="color:Blue; Private <span style="color:Blue; Delegate <span style="color:Blue; Sub DisplayInvoker(<span style="color:Blue; ByVal t <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Private <span style="color:Blue; Delegate <span style="color:Blue; Sub DisplayInvoker2()
<span style="color:Blue; Private Connected <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Private marData(1024) <span style="color:Blue; As <span style="color:Blue; Byte
<span style="color:Blue; Private mobjClient <span style="color:Blue; As TcpClient
<span style="color:Blue; Private mobjText <span style="color:Blue; As <span style="color:Blue; New StringBuilder()
<span style="color:Blue; Public Username <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Private <span style="color:Blue; Sub BuildString(<span style="color:Blue; ByVal Bytes() <span style="color:Blue; As <span style="color:Blue; Byte, <span style="color:Blue; ByVal offset <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal <span style="color:Blue; count <span style="color:Blue; As <span style="color:Blue; Integer)
<span style="color:Blue; Dim intIndex <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; For intIndex = offset <span style="color:Blue; To offset + <span style="color:Blue; count - 1
<span style="color:Blue; If Bytes(intIndex) = 10 <span style="color:Blue; Then
mobjText.Length -= 1
<span style="color:Blue; Me.Invoke(<span style="color:Blue; New DisplayInvoker(<span style="color:Blue; AddressOf <span style="color:Blue; Me.HandleText), {mobjText.ToString})
mobjText = <span style="color:Blue; New StringBuilder()
<span style="color:Blue; Else
mobjText.Append(ChrW(Bytes(intIndex)))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
DisplayText(Username + <span style="color:#A31515; ": " + TextBox2.Text)
Send(<span style="color:#A31515; "1" & TextBox2.Text)
TextBox2.Text = <span style="color:#A31515; ""
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub DisplayText(<span style="color:Blue; ByVal t <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; If <span style="color:Blue; Not TextBox1.Text = <span style="color:#A31515; "" <span style="color:Blue; Then
TextBox1.Text += vbNewLine
<span style="color:Blue; End <span style="color:Blue; If
TextBox1.AppendText(t)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub DoRead(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Blue; Dim intCount <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Try
intCount = mobjClient.GetStream.EndRead(ar)
<span style="color:Blue; If intCount < 1 <span style="color:Blue; Then
<span style="color:Blue; Me.Invoke(<span style="color:Blue; New DisplayInvoker2(<span style="color:Blue; AddressOf <span style="color:Blue; Me.MarkAsDisconnected))
<span style="color:Blue; Exit <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; If
BuildString(marData, 0, intCount)
mobjClient.GetStream.BeginRead(marData, 0, 1024, <span style="color:Blue; AddressOf DoRead, <span style="color:Blue; Nothing)
<span style="color:Blue; Catch e <span style="color:Blue; As Exception
<span style="color:Blue; Me.Invoke(<span style="color:Blue; New DisplayInvoker2(<span style="color:Blue; AddressOf <span style="color:Blue; Me.MarkAsDisconnected))
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Form2_FormClosed(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.Windows.Forms.FormClosedEventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.FormClosed
<span style="color:Blue; If Connected <span style="color:Blue; Then
mobjClient.Client.Disconnect(<span style="color:Blue; False)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Form2_Load(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
<span style="color:Blue; Try
mobjClient = <span style="color:Blue; New TcpClient(<span style="color:#A31515; "IP", port)
Connected = <span style="color:Blue; True
DisplayText(<span style="color:#A31515; "Connected to server.")
mobjClient.GetStream.BeginRead(marData, 0, 1024, <span style="color:Blue; AddressOf DoRead, <span style="color:Blue; Nothing)
Form1.Close()
Send(<span style="color:#A31515; "0" + Username)
<span style="color:Blue; Catch
MsgBox(<span style="color:#A31515; "Failed to connect to server.")
<span style="color:Blue; Me.Close()
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub HandleText(<span style="color:Blue; ByVal t <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; If t(0) = <span style="color:#A31515; "0" <span style="color:Blue; Then
DisplayText(t.Remove(0, 1) + <span style="color:#A31515; " has logged in.")
ListBox1.Items.Add(t.Remove(0, 1))
<span style="color:Blue; ElseIf t(0) = <span style="color:#A31515; "1" <span style="color:Blue; Then
DisplayText(t.Remove(0, 1).Remove(t.IndexOf(<span style="color:#A31515; "|") - 1) + <span style="color:#A31515; ": " + t.Remove(0, t.IndexOf(<span style="color:#A31515; "|") + 1))
<span style="color:Blue; ElseIf t(0) = <span style="color:#A31515; "2" <span style="color:Blue; Then
DisplayText(t.Remove(0, 1) + <span style="color:#A31515; " has logged out.")
ListBox1.Items.Remove(t.Remove(0, 1))
<span style="color:Blue; ElseIf t(0) = <span style="color:#A31515; "3" <span style="color:Blue; Then
ListBox1.Items.Add(t.Remove(0, 1))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub LogoutToolStripMenuItem_Click(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles LogoutToolStripMenuItem.Click
mobjClient.Client.Disconnect(<span style="color:Blue; False)
<span style="color:Blue; Me.Invoke(<span style="color:Blue; New DisplayInvoker2(<span style="color:Blue; AddressOf <span style="color:Blue; Me.MarkAsDisconnected))
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub MarkAsDisconnected()
<span style="color:Blue; If Connected <span style="color:Blue; Then
MsgBox(<span style="color:#A31515; "Disconnected from server.")
<span style="color:Blue; End <span style="color:Blue; If
Form1.TextBox1.Text = Username
Form1.Show()
<span style="color:Blue; Me.Close()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub MenuStrip1_MenuActivate(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles MenuStrip1.MenuActivate
Panel1.Location = <span style="color:Blue; New Point(0, 0)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub MenuStrip1_MenuDeactivate(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles MenuStrip1.MenuDeactivate
Panel1.Location = <span style="color:Blue; New Point(0, -24)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Send(<span style="color:Blue; ByVal t <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Dim w <span style="color:Blue; As <span style="color:Blue; New IO.StreamWriter(mobjClient.GetStream)
w.Write(t & vbCr)
w.Flush()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub TextBox2_KeyDown(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.Windows.Forms.KeyEventArgs) <span style="color:Blue; Handles TextBox2.KeyDown
<span style="color:Blue; If e.KeyCode = Keys.Enter <span style="color:Blue; Then
e.SuppressKeyPress = <span style="color:Blue; True
Button1.PerformClick()
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
although I do have another form it just includes a login name text box & login button which passes login name & opens the 2nd form & thats all it does.
View the full article
doesnt go to any lines of code or anything for me to find where the problem is happening at.
Now I know this isnt a server issue for sure, no bugs show up in the servers coding program at all & it shouldnt take the servers help for a client to disconnect properly between my app being closed & them using the logout button. It only
bugs when the server goes down & only after having used the logout button since youve opened the application...
Here is the code...and the bug is .... <span style="font-family:Consolas; font-size:xx-small
<span style="font-family:Consolas; font-size:xx-small
A first chance exception of type System.InvalidOperationException occurred in System.Windows.Forms.dll
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.Net.Sockets
<span style="color:Blue; Imports System.Text
<span style="color:Blue; Public <span style="color:Blue; Class Form2
<span style="color:Blue; Private <span style="color:Blue; Delegate <span style="color:Blue; Sub DisplayInvoker(<span style="color:Blue; ByVal t <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Private <span style="color:Blue; Delegate <span style="color:Blue; Sub DisplayInvoker2()
<span style="color:Blue; Private Connected <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Private marData(1024) <span style="color:Blue; As <span style="color:Blue; Byte
<span style="color:Blue; Private mobjClient <span style="color:Blue; As TcpClient
<span style="color:Blue; Private mobjText <span style="color:Blue; As <span style="color:Blue; New StringBuilder()
<span style="color:Blue; Public Username <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Private <span style="color:Blue; Sub BuildString(<span style="color:Blue; ByVal Bytes() <span style="color:Blue; As <span style="color:Blue; Byte, <span style="color:Blue; ByVal offset <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal <span style="color:Blue; count <span style="color:Blue; As <span style="color:Blue; Integer)
<span style="color:Blue; Dim intIndex <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; For intIndex = offset <span style="color:Blue; To offset + <span style="color:Blue; count - 1
<span style="color:Blue; If Bytes(intIndex) = 10 <span style="color:Blue; Then
mobjText.Length -= 1
<span style="color:Blue; Me.Invoke(<span style="color:Blue; New DisplayInvoker(<span style="color:Blue; AddressOf <span style="color:Blue; Me.HandleText), {mobjText.ToString})
mobjText = <span style="color:Blue; New StringBuilder()
<span style="color:Blue; Else
mobjText.Append(ChrW(Bytes(intIndex)))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
DisplayText(Username + <span style="color:#A31515; ": " + TextBox2.Text)
Send(<span style="color:#A31515; "1" & TextBox2.Text)
TextBox2.Text = <span style="color:#A31515; ""
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub DisplayText(<span style="color:Blue; ByVal t <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; If <span style="color:Blue; Not TextBox1.Text = <span style="color:#A31515; "" <span style="color:Blue; Then
TextBox1.Text += vbNewLine
<span style="color:Blue; End <span style="color:Blue; If
TextBox1.AppendText(t)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub DoRead(<span style="color:Blue; ByVal ar <span style="color:Blue; As IAsyncResult)
<span style="color:Blue; Dim intCount <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Try
intCount = mobjClient.GetStream.EndRead(ar)
<span style="color:Blue; If intCount < 1 <span style="color:Blue; Then
<span style="color:Blue; Me.Invoke(<span style="color:Blue; New DisplayInvoker2(<span style="color:Blue; AddressOf <span style="color:Blue; Me.MarkAsDisconnected))
<span style="color:Blue; Exit <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; If
BuildString(marData, 0, intCount)
mobjClient.GetStream.BeginRead(marData, 0, 1024, <span style="color:Blue; AddressOf DoRead, <span style="color:Blue; Nothing)
<span style="color:Blue; Catch e <span style="color:Blue; As Exception
<span style="color:Blue; Me.Invoke(<span style="color:Blue; New DisplayInvoker2(<span style="color:Blue; AddressOf <span style="color:Blue; Me.MarkAsDisconnected))
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Form2_FormClosed(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.Windows.Forms.FormClosedEventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.FormClosed
<span style="color:Blue; If Connected <span style="color:Blue; Then
mobjClient.Client.Disconnect(<span style="color:Blue; False)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Form2_Load(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
<span style="color:Blue; Try
mobjClient = <span style="color:Blue; New TcpClient(<span style="color:#A31515; "IP", port)
Connected = <span style="color:Blue; True
DisplayText(<span style="color:#A31515; "Connected to server.")
mobjClient.GetStream.BeginRead(marData, 0, 1024, <span style="color:Blue; AddressOf DoRead, <span style="color:Blue; Nothing)
Form1.Close()
Send(<span style="color:#A31515; "0" + Username)
<span style="color:Blue; Catch
MsgBox(<span style="color:#A31515; "Failed to connect to server.")
<span style="color:Blue; Me.Close()
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub HandleText(<span style="color:Blue; ByVal t <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; If t(0) = <span style="color:#A31515; "0" <span style="color:Blue; Then
DisplayText(t.Remove(0, 1) + <span style="color:#A31515; " has logged in.")
ListBox1.Items.Add(t.Remove(0, 1))
<span style="color:Blue; ElseIf t(0) = <span style="color:#A31515; "1" <span style="color:Blue; Then
DisplayText(t.Remove(0, 1).Remove(t.IndexOf(<span style="color:#A31515; "|") - 1) + <span style="color:#A31515; ": " + t.Remove(0, t.IndexOf(<span style="color:#A31515; "|") + 1))
<span style="color:Blue; ElseIf t(0) = <span style="color:#A31515; "2" <span style="color:Blue; Then
DisplayText(t.Remove(0, 1) + <span style="color:#A31515; " has logged out.")
ListBox1.Items.Remove(t.Remove(0, 1))
<span style="color:Blue; ElseIf t(0) = <span style="color:#A31515; "3" <span style="color:Blue; Then
ListBox1.Items.Add(t.Remove(0, 1))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub LogoutToolStripMenuItem_Click(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles LogoutToolStripMenuItem.Click
mobjClient.Client.Disconnect(<span style="color:Blue; False)
<span style="color:Blue; Me.Invoke(<span style="color:Blue; New DisplayInvoker2(<span style="color:Blue; AddressOf <span style="color:Blue; Me.MarkAsDisconnected))
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub MarkAsDisconnected()
<span style="color:Blue; If Connected <span style="color:Blue; Then
MsgBox(<span style="color:#A31515; "Disconnected from server.")
<span style="color:Blue; End <span style="color:Blue; If
Form1.TextBox1.Text = Username
Form1.Show()
<span style="color:Blue; Me.Close()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub MenuStrip1_MenuActivate(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles MenuStrip1.MenuActivate
Panel1.Location = <span style="color:Blue; New Point(0, 0)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub MenuStrip1_MenuDeactivate(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles MenuStrip1.MenuDeactivate
Panel1.Location = <span style="color:Blue; New Point(0, -24)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Send(<span style="color:Blue; ByVal t <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Dim w <span style="color:Blue; As <span style="color:Blue; New IO.StreamWriter(mobjClient.GetStream)
w.Write(t & vbCr)
w.Flush()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub TextBox2_KeyDown(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.Windows.Forms.KeyEventArgs) <span style="color:Blue; Handles TextBox2.KeyDown
<span style="color:Blue; If e.KeyCode = Keys.Enter <span style="color:Blue; Then
e.SuppressKeyPress = <span style="color:Blue; True
Button1.PerformClick()
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
although I do have another form it just includes a login name text box & login button which passes login name & opens the 2nd form & thats all it does.
View the full article