When I try to cancel an async ADO connection to some DB server that is offline (or not responding), the Cancel method of the ADODB.Connection object blocks for the set time-out period.
I do the async connection like this:
Set Connection = New ADODB.Connection
Connection.Provider = "SQLOLEDB"
Connection.ConnectionTimeout = 60
Connection.ConnectionString = "Initial Catalog=" & RTrim(DBName) & _
";Data Source=" & RTrim(DBServerName) & ";Integrated Security = SSPI"
Connection.Open , , , adAsyncConnect
And then later call the following to cancel/close the connection:
If (Connection.State And adStateConnecting) = adStateConnecting Then
' ==== CONNECTION BLOCKS HERE ======
Connection.Cancel
End If
If (Connection.State And adStateOpen) = adStateOpen Then
Connection.Close
End If
Set Connection = Nothing
Is there a way to not let the Cancel method block?
I found my own solution at the end. Well, at least an acceptable workaround.
First I created a module that could cancel/close the connection in a timer (thanks to an idea from a Code Project article):
Option Explicit
' Timer API:
Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, _
ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) _
As Long
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, _
ByVal nIDEvent As Long) As Long
' Collection of connections to cancel
Private m_connections As Collection
' The ID of our API Timer:
Private m_lTimerID As Long
Private Sub TimerProc(ByVal lHwnd As Long, ByVal lMsg As Long, _
ByVal lTimerID As Long, ByVal lTime As Long)
On Error GoTo ErrH:
Dim cnx As ADODB.Connection
' Remove the timer
KillTimer 0, lTimerID
If Not m_connections Is Nothing Then
With m_connections
Do While .Count > 0
Set cnx = .Item(1)
.Remove 1
TryCancelOrCloseConnection cnx
Loop
End With
If m_connections.Count = 0 Then
Set m_connections = Nothing
End If
End If
' Let the next call to CancelOrCloseAsync create a new timer
m_lTimerID = 0
Exit Sub
ErrH:
' Let the next call to CancelOrCloseAsync create a new timer
m_lTimerID = 0
Debug.Print "Error closing connetions timer: " & Err.Description
End Sub
Private Sub TryCancelOrCloseConnection(cnx As ADODB.Connection)
On Error GoTo ErrH
If Not cnx Is Nothing Then
If (cnx.State And adStateConnecting) = adStateConnecting Then
' The call to Cancel here blocks this execution path (until connection time-out),
' but we assume it internally calls DoEvents, because (even though it blocks here) messages get pumped.
cnx.Cancel
End If
' If the connection actually made it to an open state, we make sure it is closed
If (cnx.State And adStateOpen) = adStateOpen Then
cnx.Close
End If
End If
Exit Sub
ErrH:
Debug.Print "ADO Connection Cancel/Close error " & Err.Description
' We deliberately suppress the error here.
' The reason is that accessing the Connection.State property, while there was an error when
' connecting, will raise an error. The idea of this method is simply to make sure we close/cancel
' the pending connection if there was no connection error.
End Sub
Public Sub CancelOrCloseAsync(cnx As ADODB.Connection)
If Not cnx Is Nothing Then
' Add cnx to the collection of connections to cancel
If m_connections Is Nothing Then
Set m_connections = New Collection
End If
m_connections.Add cnx
' Create a timer to start cancelling the connection(s), but only if one is not already busy
' We need to cast the process off to a timer because the Connection.Cancel blocks the
' normal execution path.
If m_lTimerID = 0 Then
m_lTimerID = SetTimer(0, 0, 1, AddressOf TimerProc)
End If
End If
End Sub
I then created a Connection surrogate class called clsADOAsyncConn
Private WithEvents Connection As ADODB.Connection
Private m_Pending As Boolean
Public Event ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)
Public Property Get Provider() As String
Provider = Connection.Provider
End Property
Public Property Let Provider(ByVal val As String)
Connection.Provider = val
End Property
Public Property Get ConnectionTimeout() As Long
ConnectionTimeout = Connection.ConnectionTimeout
End Property
Public Property Let ConnectionTimeout(ByVal val As Long)
Connection.ConnectionTimeout = val
End Property
Public Property Get ConnectionString() As String
ConnectionString = Connection.ConnectionString
End Property
Public Property Let ConnectionString(ByVal val As String)
Connection.ConnectionString = val
End Property
Public Sub OpenAsync(Optional ByVal UserID As String = "", Optional ByVal Password As String = "")
Connection.Open , UserID, Password, adAsyncConnect
m_Pending = True
End Sub
Private Sub Class_Initialize()
Set Connection = New ADODB.Connection
End Sub
Private Sub Class_Terminate()
If Not Connection Is Nothing And m_Pending Then
' While the connection is still pending, when the user of this class reminates the refernce
' of this class, we need to cancel it in its own timer loop or else the caller's code will
' block at the point where the refernce to this object is de-referenced.
CancelOrCloseAsync Connection
End If
End Sub
Private Sub Connection_ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)
m_Pending = False
' Notify the object client of the connection state
RaiseEvent ConnectComplete(pError, adStatus, pConnection)
End Sub
I then update my original connection code to this:
Set Connection = New clsADOAsyncConn
Connection.Provider = "SQLOLEDB"
Connection.ConnectionTimeout = 60
Connection.ConnectionString = "Initial Catalog=" & RTrim(DBName) & _
";Data Source=" & RTrim(DBServerName) & ";Integrated Security = SSPI"
Connection.OpenAsync
The actual connection is then retuned by the clsADOAsyncConn.ConnectComplete event.
The only known issue with this solution is that even though it helps prevent a block in normal execution of code, it still causes a block when the process exits (at least until the last pending connection(s) times out)
Related
I am simulating an ATM in Visual Basic. I have a table called Authentication in SQL. The Table contains two columns: The NUM_CARD column and the PIN_CARD column. I need to match row (0) column 1, row (1) column (1), row (2) column (1), and so on with the other rows as the card IDs are inserted. How can I do that? Thanks in advance.
The class DBConnection is the following:
Imports System
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class clsDBConnection
'Class variables'
Public cn As SqlConnection
Public cmd As SqlCommand
Public dr As SqlDataReader
'Constructor of the Connection class that creates the connection'
Sub New()
Try
cn = New SqlConnection("Data Source=JOVALLES-PC\SQLSERVEREX;Initial Catalog=SigmasBank;Integrated Security=True")
cn.Open()
Catch ex As Exception
MsgBox("Error connecting due to:: " + ex.ToString)
End Try
End Sub
'Returns true or false if the record exists or not in the database'
Function validationAutentication_p1(ByVal NUM_CARD As String) As Boolean
Dim result As Boolean = False
Try
cmd = New SqlCommand("Select * from Autentication where NUM_CARD='" & NUM_CARD & "'", cn)
dr = cmd.ExecuteReader
If dr.HasRows Then
result = True
End If
dr.Close()
Catch ex As Exception
MsgBox("Error in the procedure: " + ex.ToString)
End Try
Return result
End Function
Function validationAutentication_p2(ByVal PIN_CARD As String) As Boolean
Dim result As Boolean = False
Try
cmd = New SqlCommand("Select * from Autentication where PIN_CARD='" & PIN_CARD & "'", cn)
dr = cmd.ExecuteReader
If dr.HasRows Then
result = True
End If
dr.Close()
Catch ex As Exception
MsgBox("Error in the procedure: " + ex.ToString)
End Try
Return result
End Function
End Class
Insert Card ID Form:
Public Class FRM_InsertCardID
Public conn As New clsDBConnection
Private Sub BTN_Ok_Click(sender As Object, e As EventArgs) Handles BTN_Ok.Click
If TXB_CardID.Text.Length = 0 Then
MsgBox("Please fill in field.")
ElseIf TXB_CardID.Text.Length > 0 And TXB_CardID.Text.Length < 16 Then
MsgBox("Your Card ID must be 16 digits.")
ElseIf conn.validationAutentication_p1(TXB_CardID.Text) = False Then
MsgBox("The Card ID doesn't exist.")
Else
FRM_PIN.Show()
Me.Hide()
TXB_CardID.Text = ""
End If
End Sub
Insert PIN form:
Public Class FRM_PIN
Public conn As New clsDBConnection
Private Sub BTN_Ok_Click(sender As Object, e As EventArgs) Handles BTN_Ok.Click
If TXB_PIN.Text.Length = 0 Then
MsgBox("Please fill in field.")
ElseIf TXB_PIN.Text.Length > 0 And TXB_PIN.Text.Length < 4 Then
MsgBox("Your PIN must be 4 digits.")
ElseIf conn.validationAutentication_p2(TXB_PIN.Text) = False Then
MsgBox("Incorrect PIN Please try again.")
Else
FRM_Transaction.Show()
Me.Hide()
TXB_PIN.Text = ""
End If
End Sub
Not sure if typo causing issue otherwise?? - - Authentication
"I have a table called Authentication in SQL. "
" cmd = New SqlCommand("Select * from Autentication where PIN_CARD='" & PIN_CARD & "'", cn)"
Let's start with clsDBConnection. You do not need to import System. That is there by default. System.Data.Sql is never used. Get rid of that too.
One would think that this class is about a database connection. It is not. It contains code for authentication. So rename; something like DataAccess.
Never make connections, commands and readers class level variables. These database objects need to be closed and disposed so the class is not where to declare them. They need to be local variables, local to the method where they are used.
Never, never open a connection until directly before it is used. Ideally the line before an .Execute... method is called. Be sure it is also closed and disposed as soon as possible. Your code opens a connection and leaves it flapping in the breeze.
What you can do in a DataAccess class is make your connection string a Private class level variable. Private cnString as String = ...
I can't see where you would need a custom constructor at all. Just get rid of Sub New() I have made the 2 methods in your class Shared This data is shared by all instances of the class and you do not have declare an instance of the class to use these methods. You can call shared methods just by referencing the name of the class and the method. Also the conString is Shared because it is used by shared methods.
I decided that the pin number is not necessarily unique since they only go up to 9999. That is why I used 2 parameters for the second method.
Note:
I had to guess at the datatype and field size of the SqlParameters. Check your database and adjust the code accordingly.
Public Class FRM_InsertCardID
Private Sub BTN_Ok_Click(sender As Object, e As EventArgs) Handles BTN_Ok.Click
If TXB_CardID.Text.Length = 0 Then
MsgBox("Please fill in field.")
'Don't give the user any information on what a proper card ID consists of
Return
End If
If DataAccess.validationAutentication_p1(TXB_CardID.Text) = False Then
MsgBox("The Card ID doesn't exist.")
Else
FRM_PIN.Show()
'It appears you are using the default instance of FRM_PIN
FRM_PIM.CardID = TXB_CardID.Text
TXB_CardID.Text = ""
Me.Hide()
End If
End Sub
End Class
Public Class FRM_PIN
Friend CardID As String
Private Sub BTN_Ok_Click(sender As Object, e As EventArgs) Handles BTN_Ok.Click
If TXB_PIN.Text.Length = 0 Then
MsgBox("Please fill in field.")
Return 'Exits the sub
End If
If DataAccess.validationAutentication_p2(CardID, TXB_PIN.Text) = False Then
MsgBox("Incorrect PIN Please try again.")
Else
TXB_PIN.Text = ""
FRM_Transaction.Show()
Me.Hide()
End If
End Sub
End Class
Public Class DataAccess
Private Shared conString As String = "Data Source=JOVALLES-PC\SQLSERVEREX;Initial Catalog=SigmasBank;Integrated Security=True"
Public Shared Function validationAutentication_p1(ByVal NUM_CARD As String) As Boolean
Dim result = False
Using cn As New SqlConnection(conString),
cmd As New SqlCommand("Select * from Autentication where NUM_CARD= #NumCARD;", cn)
cmd.Parameters.Add("#NumCard", SqlDbType.VarChar, 16).Value = NUM_CARD
cn.Open()
Using dr = cmd.ExecuteReader
If dr.HasRows Then
result = True
End If
End Using
End Using
Return result
End Function
Public Shared Function validationAutentication_p2(ByVal CardID As String, ByVal PIN_CARD As String) As Boolean
Dim result = False
Using cn As New SqlConnection(conString),
cmd As New SqlCommand("Select * From Autentication where NUM_CARD = #NumCard AND PIN_CARD=#PinCard;", cn)
cmd.Parameters.Add("#NumCard", SqlDbType.VarChar, 100).Value = CardID
cmd.Parameters.Add("#PinCard", SqlDbType.VarChar, 4).Value = PIN_CARD
cn.Open()
Using dr = cmd.ExecuteReader()
If dr.HasRows Then
result = True
End If
End Using
End Using
Return result
End Function
End Class
I'm trying to use serial communication in WPF with vb.net and the only successful thing that I can do is sending data. When it comes to receiving data I have received different errors, and this one is the hardest for me to get over with. I tried to apply the windows form code when we want to receive data by using the Control.InvokeRequired, but since this is WPF this was transferred to Dispatcher.CheckAccess(). Everytime I load the program the transmission is ok but reception gives me error of System.InvalidOperation:'The calling thread cannot acces this object because a different thread owns it.' I looked at other solutions but none have worked.
Are there other solutions/process for receiving data from the other side?
I tried various examples from both windows document and other sites, but none was successful in WPF application.
Imports System.IO.Ports
Imports System.Text
Imports System.Threading
Imports System.Windows
Class MainWindow
Private WithEvents serport As New SerialPort
Private Delegate Sub SetTextCallback(ByVal [text] As String)
Private Sub onLoad()
Dim ports As String() = SerialPort.GetPortNames()
Dim port As String
For Each port In ports
comPortCombo.Items.Add(port)
Next port
End Sub
Private Sub ConButton_Click(sender As Object, e As RoutedEventArgs) Handles conButton.Click
'serport = New SerialPort()
If (comPortCombo.Text = "") Then
MessageBox.Show("Please select COM port!")
Exit Sub
End If
serport.PortName = comPortCombo.Text
serport.BaudRate = baudCombo.Text
serport.Open()
transButton.IsEnabled = True
conButton.IsEnabled = False
disconButton.IsEnabled = True
End Sub
Private Sub DisconButton_Click(sender As Object, e As RoutedEventArgs) Handles disconButton.Click
serport.Close()
disconButton.IsEnabled = False
transButton.IsEnabled = False
conButton.IsEnabled = True
End Sub
Private Sub TransButton_Click(sender As Object, e As RoutedEventArgs) Handles transButton.Click
serport.Write(transTextBox.Text & vbCrLf)
transTextBox.Clear()
End Sub
Private Sub serport_datareceived(sender As Object, e As SerialDataReceivedEventArgs) Handles serport.DataReceived
'recTextBox.Text = receiveserialdata()
'with serForm's function???
ReceivedText(serport.ReadLine())
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If recTextBox.Dispatcher.CheckAccess() Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Dispatcher.Invoke(x, New Object() {(text)})
Else
recTextBox.Text &= [text]
End If
End Sub
'Function receiveserialdata() As String
' ' receive strings from a serial port.
' Dim returnstr As String = ""
' Try
' Do
' Dim incoming As String = serport.ReadExisting()
' If incoming Is Nothing Then
' Exit Do
' Else
' returnstr &= incoming & vbCrLf
' End If
' Loop
' Catch ex As TimeoutException
' returnstr = "error: serial port read timed out."
' Finally
' If serport IsNot Nothing Then serport.Close()
' End Try
' Return returnstr
'End Function
End Class
System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.'
Quick question really, do I need to close an ADO command? I have read in several places that simply setting it to nothing does the same thing as closing it but I'm yet to find a definitive answer.
I have a VB6 routine that at runtime creates a connection and command object which execute asynchronously a stored procedure that doesn't return any results.
At the end of this routine both objects are set to nothing. The below code shows exactly what's performed
' Open connection
con.Open
'
' Create command to execute stored proc
Set cmd = New ADODB.Command
cmd.ActiveConnection = con
cmd.CommandType = adCmdStoredProc
cmd.CommandText = cSQLDelete
cmd.Parameters.Append cmd.CreateParameter _
("#ExpiryDate=", adDate, adParamInput, 20, ExpiryDate)
'
' Run procedure, no results
cmd.Execute , , adExecuteNoRecords + adAsyncExecute
'
' Tidy up
Set cmd = Nothing
Set con = Nothing
Note the absence of con.Close. If this is inserted before the con=nothing, the stored procedure does not run - I assume that since it's asynchronous it hasn't had time to execute before the connection is closed.
Without the con.Close, this works fine.
So, my question is what implications are there for not closing the connection. Will the connection simply timeout in the background?? Bear in mind that this function is called A LOT and so far testing hasn't shown any problems.
If I need to close the connection when the stored procedure finishes, how would I do this? Having a C# background, I'm unfamiliar with VB6 and find creating objects with events at runtime an awkward process.
p.s. Sql Server 2008
Thanks
You have to wait for command to complete or fail before tearing down the connection. Closing connection while command is executing will instantaneously raise an error, setting connection to nothing won't release the resources it is holding and may crash your program.
The correct method is to close the connection when the command completes, usually by monitoring the ExecuteComplete event (your connection must belong to a class or a form).
You can use a something similar:
Option Explicit
Dim WithEvents con As ADODB.Connection
Dim bExecuting As Boolean
Private Sub cmdCancel_Click()
If Not bExecuting Then Exit Sub
If Not con Is Nothing Then
con.Cancel
End If
bExecuting = False
End Sub
Private Sub cmdExecute_Click()
If bExecuting Then Exit Sub
If con Is Nothing Then
Set con = New ADODB.Connection
con.Open "Provider=..."
End If
bExecuting = True
con.Execute "WAITFOR DELAY '000:00:10'", , adExecuteNoRecords + adAsyncExecute
End Sub
Private Sub cmdExit_Click()
If bExecuting Then Exit Sub
Unload Me
End Sub
Private Sub con_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
If Not pError Is Nothing Then Debug.Print pError.Description
bExecuting = False
End Sub
Private Sub Form_Load()
bExecuting = False
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If bExecuting Then Cancel = 1
End Sub
Private Sub Form_Unload()
If Not con Is Nothing Then
If con.State = adStateOpen Then con.Close
set con = Nothing
End If
End Sub
What this code does is execute the command if the connection is not busy (State=Open). Otherwise put it on a stack and execute it when the current command completes. My knowledge of VB arrays is limited so sorry its a bit ugly - there may be a better way to do a FIFO queue?
Dim WithEvents mobjAdoConn As ADODB.Connection
Dim CommandArray() As String
Private Sub Command1_Click()
Dim cmdNo As Integer
If mobjAdoConn.State <> adStateOpen Then
cmdNo = UBound(CommandArray)
ReDim Preserve CommandArray(cmdNo + 1)
CommandArray(cmdNo) = "WAITFOR DELAY '000:00:10'"
Label2.Caption = cmdNo
Else
mobjAdoConn.Execute "WAITFOR DELAY '000:00:10'", , adExecuteNoRecords + adAsyncExecute
End If
End Sub
Private Sub Form_Load()
On Error GoTo Err
Set mobjAdoConn = New ADODB.Connection
mobjAdoConn.ConnectionTimeout = 30
mobjAdoConn.ConnectionString = "Provider..."
mobjAdoConn.Open
ReDim CommandArray(1)
Exit Sub
Err:
MsgBox Err.Description, vbOKOnly
End Sub
Private Sub mobjAdoConn_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
Dim cmd As String
Dim i As Integer
Dim cmds As Integer
cmds = UBound(CommandArray)
If cmds > 1 Then
cmd = CommandArray(1)
If cmds = 2 Then
ReDim CommandArray(1)
Label2.Caption = 0
Else
For i = 2 To cmds - 1
CommandArray(i - 1) = CommandArray(i)
Next i
ReDim Preserve CommandArray(cmds - 1)
Label2.Caption = cmds - 2
End If
mobjAdoConn.Execute cmd, , adExecuteNoRecords + adAsyncExecute
End If
End Sub
I'm here because I'm considering using asynchExecute on a similar real-time application. The problem with setting the connection to nothing while it is open is that it may not get properly released and every 10Ms you create a new one, this could result in a memory leak. Mucker you should monitor memory usage when you are testing this to see if it happens.
I'm building an application that can communicate and send commands to game servers. In the gaming world we call this “rcon” for remote console, but in reality it's just a telnet session. After successful authentication, a text command can be issued to the server and a text response is sent back to the client.
Often times server admins/owners run multiple game servers and every server has its own distinct ip address, port & password. I would like my application to loop through a list/array/datatable/dictionary (or whatever method is best ) of servers, connect and authenticate to each server, then somehow store that connection so commands can be sent to it in the future (Without having to connect & authenticate again)
Below is the code I have that works for a single server. I store the TcpClient(client) & NetworkStream(stm) as global objects which keeps the connection/session open to the server. What is the best method for me to do this with multiple servers since each server will have it's own TcpClient & NetworkStream?
Is there some way for me to store each TcpClient(client) & NetworkStream(stm) in an array, dictionary or something? I'm completely lost with how to approach this.
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Security.Cryptography
Imports System.IO
Public Class Form1
Public client As TcpClient
Public stm As NetworkStream
Sub SendCmd(cmd As String)
Dim data
If cmd.Trim = "" Then
data = Encoding.GetEncoding(1252).GetBytes(cmd)
Else
data = Encoding.GetEncoding(1252).GetBytes(cmd & vbCrLf)
End If
stm.Write(data, 0, data.Length)
Dim resp As Byte() = New Byte(512) {}
Dim memStream = New MemoryStream()
Thread.Sleep(500)
Dim bytes As Integer = 0
If stm.DataAvailable Then
Do
Thread.Sleep(10)
bytes = stm.Read(resp, 0, resp.Length)
memStream.Write(resp, 0, bytes)
Loop While stm.DataAvailable
Dim responsedata As String = Encoding.GetEncoding(1252).GetString(memStream.ToArray())
If responsedata.Contains("### Digest seed: ") Then
'The server is asking for authentication: Login to the server now
Authenticate(responsedata)
End If
OutputRTB.Text &= responsedata
End If
memStream.Close()
End Sub
Sub GetPlayerList()
If stm.CanRead Then
Dim data = Encoding.GetEncoding(1252).GetBytes("bf2cc pl" & vbCrLf)
Dim PlayerDT As New DataTable
PlayerDT.Columns.Add("PlayerSlot", GetType(Integer))
PlayerDT.Columns.Add("HeroName", GetType(String))
PlayerDT.Columns.Add("Score", GetType(String))
PlayerDT.Columns.Add("HeroID", GetType(String))
PlayerDT.Columns.Add("PlayerID", GetType(String))
PlayerDT.Columns.Add("Level", GetType(Integer))
PlayerDT.Columns.Add("Class", GetType(String))
PlayerDT.Columns.Add("Ping", GetType(Integer))
stm.Write(data, 0, data.Length)
Dim resp As Byte() = New Byte(512) {}
Dim memStream = New MemoryStream()
Thread.Sleep(500)
Dim bytes As Integer = 0
If stm.DataAvailable Then
Do
Thread.Sleep(10)
bytes = stm.Read(resp, 0, resp.Length)
memStream.Write(resp, 0, bytes)
Loop While stm.DataAvailable
Dim responsedata As String = Encoding.GetEncoding(1252).GetString(memStream.ToArray())
If responsedata.Contains("### Digest seed: ") Then
'Login to the server
Authenticate(responsedata)
End If
OutputRTB.Text = responsedata.Replace(vbTab, "^")
Dim Rows() As String = responsedata.Split(Environment.NewLine)
For i = 0 To Rows.Length - 1
Dim Cols() As String = Rows(i).Split(vbTab)
If Cols.Length > 40 Then
PlayerDT.Rows.Add(Cols(0).ToString(), Cols(1).ToString(), Cols(37).ToString(), Cols(10).ToString(), Cols(47).ToString(), Cols(39).ToString(), Cols(34).ToString(), Cols(3).ToString)
End If
Next
End If
DataGridView1.DataSource = PlayerDT
DataGridView1.Update()
memStream.Close()
End If
End Sub
Sub Authenticate(ByVal rdata As String)
Dim DigestKeyStart As Integer = rdata.LastIndexOf(":") + 2
Dim DigestKeyLen As Integer = 16
Dim PWResponse As String = rdata.Substring(DigestKeyStart, DigestKeyLen) & PassTXT.Text
PWResponse = "login " & GenerateHash(PWResponse)
SendCmd(PWResponse)
End Sub
Private Function GenerateHash(ByVal SourceText As String) As String
Dim objMD5 As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim arrData() As Byte
Dim arrHash() As Byte
' first convert the string to bytes (using UTF8 encoding for unicode characters)
arrData = System.Text.Encoding.UTF8.GetBytes(SourceText)
' hash contents of this byte array
arrHash = objMD5.ComputeHash(arrData)
' thanks objects
objMD5 = Nothing
' return formatted hash
Return ByteArrayToString(arrHash)
End Function
Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
Dim strOutput As New System.Text.StringBuilder(arrInput.Length)
For i As Integer = 0 To arrInput.Length - 1
strOutput.Append(arrInput(i).ToString("X2"))
Next
Return strOutput.ToString().ToLower
End Function
Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
stm.Close()
client.Close()
End Sub
Private Sub ConnectBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectBTN.Click
client = New TcpClient(ServerIPTXT.Text, PortTXT.Text)
stm = client.GetStream()
SendCmd(CommandTXT.Text)
End Sub
Private Sub SendCommandBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendCommandBTN.Click
SendCmd(CommandTXT.Text)
End Sub
Private Sub GetPlayerListBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetPlayerListBTN.Click
GetPlayerList()
End Sub
End Class
BTW This is my first post on stackoverflow, but I have learned so much from this site over the years =)
I have recently started encountering Database connection issues with SQL Server on my development machine.
System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool
How can I monitor the connection pool to figure out what is happening?
Further Info:
I haven't had much luck with this - I'm definitely not leaking connections. Every connection is inside a using statement.
When the problem does occur, I have the Performance Monitor window open and it's not showing anywhere near the limit of the pool (which is 100) - generally around 2 - 5 connections, so I don't think the pool is being exhausted so maybe it's a timeout.
However, I have set the ConnectionTimeout to 0 - which, according to the documentation, means it should wait forever to connect - but I'm not seeing this.
When it does occur, it happens fairly quickly - I'm running under the debugger from VS2010 - starting a new instance of my application - and it might happen within a second or two of starting - in starting up the app there are several queries that happen. The actual SQL Server I'm running against is SQL Express 2008. Maybe I should try running it against SQL Server 2008 and see if I see any different behaviour.
Any other ideas?
Take a look at the ADO.NET Performance Counters related to pooling.
Your described symptom is often an indication that you are leaking connections. Make sure all connections are disposed when you are finished with them, preferably by wrapping in an using statement.
here's some code to try the pool and then failover to unpooled:
use this sub if a problem happens with the pool:
Public Sub OpenConn()
Dim sTempCNString As String = cn.ConnectionString
Try
' add a timeout to the cn string, following http://www.15seconds.com/issue/040830.htm
Dim iTimeOut As Integer = utils_Configuration.Get_ConfigInt("DBConnectTimeout", 0)
If (iTimeOut > 0 And Not cn.ConnectionString.ToLower.Contains("timeout")) Then
Diagnostics.Debug.Print("<><><><><><><> SHORT CONNECT WITH POOLING <><><><><><><><><> ")
cn.ConnectionString += ";Connect Timeout=" & iTimeOut.ToString() & ";"
End If
cn.Open()
IsOperational = True
Catch ex As Exception
Diagnostics.Debug.Print("ERROR IN OPENING, try no pool")
' see http://www.15seconds.com/issue/040830.htm
' turn off pooling
Diagnostics.Debug.Print("<><><><><><><> CONNECT WITHOUT POOLING <><><><><><><><><> ")
Dim sAddOn As String = ";Pooling=false;Connect Timeout=45;"
cn.ConnectionString = sTempCNString & sAddOn
cn.ConnectionString = cn.ConnectionString.Replace(";;", ";")
cn.Open()
End Try
End Sub
Here's some code to monitor the pool:
Option Explicit On
Option Strict On
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
' ref: http://msdn2.microsoft.com/en-us/library/ms254503.aspx
Public Class utils_SqlPerfMon
Private PerfCounters(9) As PerformanceCounter
Private connection As SqlConnection
Public sConnectString As String = ""
Public sResult As String = ""
Public Sub New()
sConnectString = Tools.GetMainDBConn().ConnectionString
connection = New SqlConnection(sConnectString)
Exec()
End Sub
Public Sub New(ByVal strC As String)
sConnectString = strC
connection = New SqlConnection(sConnectString)
Exec()
End Sub
Public Sub Exec()
Me.SetUpPerformanceCounters()
Diagnostics.Debug.Print("Available Performance Counters:")
' Create the connections and display the results.
Me.CreateConnectionsAndDisplayResults()
End Sub
Private Sub CreateConnectionsAndDisplayResults()
' List the Performance counters.
WritePerformanceCounters()
Dim connection1 As SqlConnection = New SqlConnection( _
Me.sConnectString)
connection1.Open()
Diagnostics.Debug.Print("Opened the 1st Connection:")
WritePerformanceCounters()
connection1.Close()
Diagnostics.Debug.Print("Closed the 1st Connection:")
WritePerformanceCounters()
Return
End Sub
Private Enum ADO_Net_Performance_Counters
NumberOfActiveConnectionPools
NumberOfReclaimedConnections
HardConnectsPerSecond
HardDisconnectsPerSecond
NumberOfActiveConnectionPoolGroups
NumberOfInactiveConnectionPoolGroups
NumberOfInactiveConnectionPools
NumberOfNonPooledConnections
NumberOfPooledConnections
NumberOfStasisConnections
' The following performance counters are more expensive to track.
' Enable ConnectionPoolPerformanceCounterDetail in your config file.
' SoftConnectsPerSecond
' SoftDisconnectsPerSecond
' NumberOfActiveConnections
' NumberOfFreeConnections
End Enum
Private Sub SetUpPerformanceCounters()
connection.Close()
Me.PerfCounters(9) = New PerformanceCounter()
Dim instanceName As String = GetInstanceName()
Dim apc As Type = GetType(ADO_Net_Performance_Counters)
Dim i As Integer = 0
Dim s As String = ""
For Each s In [Enum].GetNames(apc)
Me.PerfCounters(i) = New PerformanceCounter()
Me.PerfCounters(i).CategoryName = ".NET Data Provider for SqlServer"
Me.PerfCounters(i).CounterName = s
Me.PerfCounters(i).InstanceName = instanceName
i = (i + 1)
Next
End Sub
Private Declare Function GetCurrentProcessId Lib "kernel32.dll" () As Integer
Private Function GetInstanceName() As String
'This works for Winforms apps.
'Dim instanceName As String = _
' System.Reflection.Assembly.GetEntryAssembly.GetName.Name
' Must replace special characters like (, ), #, /, \\
Dim instanceName As String = _
AppDomain.CurrentDomain.FriendlyName.ToString.Replace("(", "[") _
.Replace(")", "]").Replace("#", "_").Replace("/", "_").Replace("\\", "_")
'For ASP.NET applications your instanceName will be your CurrentDomain's
'FriendlyName. Replace the line above that sets the instanceName with this:
'instanceName = AppDomain.CurrentDomain.FriendlyName.ToString.Replace("(", "[") _
' .Replace(")", "]").Replace("#", "_").Replace("/", "_").Replace("\\", "_")
Dim pid As String = GetCurrentProcessId.ToString
instanceName = (instanceName + ("[" & (pid & "]")))
Diagnostics.Debug.Print("Instance Name: {0}", instanceName)
Diagnostics.Debug.Print("---------------------------")
Return instanceName
End Function
Private Sub WritePerformanceCounters()
Dim sdelim As String = vbCrLf ' "<br>"
Diagnostics.Debug.Print("---------------------------")
sResult += "---------------------------"
sResult += sdelim
Dim strTemp As String = ""
For Each p As PerformanceCounter In Me.PerfCounters
Try
Diagnostics.Debug.Print("{0} = {1}", p.CounterName, p.NextValue)
strTemp = p.CounterName & "=" & p.NextValue.ToString
Catch ex As Exception
strTemp = ""
End Try
sResult += strTemp
sResult += sdelim
Next
Diagnostics.Debug.Print("---------------------------")
sResult += "---------------------------"
sResult += sdelim
End Sub
Private Shared Function GetSqlConnectionStringDifferent() As String
' To avoid storing the connection string in your code,
' you can retrive it from a configuration file.
Return ("Initial Catalog=AdventureWorks;Data Source=.\SqlExpress;" & _
"User Id=LowPriv;Password=Data!05;")
End Function
End Class