Find all our projects in development below.
All source code is GNU General Public License (GPL)
Browsing frmSendRequest.vb (4.81 KB)
Option Explicit On
Imports System.Threading
Public Class frmSendRequest
Private tcpClient As clsTcpClient = Nothing
Private workerThread As Thread = Nothing
Private callerHost As String = ""
Private callerPort As Integer = 0
Public Delegate Sub ResponseReceivedSub(ByVal responseData As String)
Public Sub New(Optional ByVal host As String = "", Optional ByVal port As Integer = 0)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
callerHost = host
callerPort = port
End Sub
Private Sub _showWaitCursor(ByVal bShow As Boolean)
' show/hide the hourglass
If bShow Then
Me.Cursor = Cursors.WaitCursor
Me.UseWaitCursor = True
Else
Me.UseWaitCursor = False
Me.Cursor = Cursors.Arrow
End If
End Sub
Public Sub SendRequestToClient(ByVal obj As Object)
Dim requestData As String = CType(obj, String)
Dim responseData As String = tcpClient.SendAndWait(requestData)
ResponseReceived(responseData)
End Sub
Public Sub ResponseReceived(ByVal responseData As String)
If Me.InvokeRequired Then
Me.Invoke(New ResponseReceivedSub(AddressOf ResponseReceived), responseData)
Else
workerThread = Nothing
txtResponse.Text = frmMain.ReadParsedPacket(responseData)
If Not (tcpClient Is Nothing) Then
tcpClient.Dispose()
tcpClient = Nothing
End If
_showWaitCursor(False)
txtHost.Enabled = True
txtPort.Enabled = True
btnSend.Enabled = True
txtRequest.Enabled = True
txtRequest.SelectAll()
txtRequest.Focus()
lblStatus.Text = "Ready"
End If
End Sub
Private Sub frmSendRequest_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not (workerThread Is Nothing) Then
workerThread.Abort()
workerThread = Nothing
End If
If Not (tcpClient Is Nothing) Then
tcpClient.Dispose()
tcpClient = Nothing
End If
End Sub
Private Sub frmSendRequest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If callerHost <> "" Then txtHost.Text = callerHost
If callerPort > 0 Then txtPort.Text = callerPort.ToString()
End Sub
Private Sub frmSendRequest_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
txtHost.SelectAll()
txtHost.Focus()
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If txtHost.Text.Trim() = "" Then
MessageBox.Show("Please enter the host.", Application.ProductName, _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtHost.SelectAll()
txtHost.Focus()
Exit Sub
ElseIf Not IsNumeric(txtPort.Text) Then
MessageBox.Show("Please enter the port number.", Application.ProductName, _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtPort.SelectAll()
txtPort.Focus()
Exit Sub
End If
If Not (tcpClient Is Nothing) Then
' dispose of previous tcpclient
tcpClient.Dispose()
tcpClient = Nothing
End If
txtHost.Enabled = False
txtPort.Enabled = False
btnSend.Enabled = False
txtRequest.Enabled = False
_showWaitCursor(True)
lblStatus.Text = "Connecting..."
Application.DoEvents()
Try
tcpClient = New clsTcpClient(txtHost.Text.Trim(), CInt(txtPort.Text))
workerThread = New Thread(AddressOf SendRequestToClient)
workerThread.IsBackground = True
workerThread.Start(txtRequest.Text)
lblStatus.Text = "Sending..."
Catch ex As Exception
_showWaitCursor(False)
MessageBox.Show("Unable to connect to host." + vbCrLf + vbCrLf + _
"Reason: " + ex.Message, Application.ProductName, _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtHost.Enabled = True
txtPort.Enabled = True
btnSend.Enabled = True
txtRequest.Enabled = True
txtHost.SelectAll()
txtHost.Focus()
lblStatus.Text = "Ready"
End Try
End Sub
End Class