Can anyone tell me what is wrong with the following Code.
I am trying to launch a form on a different thread because the form takes 10+ seconds to load. The code I am using follows:
Public Class frmAdjustments
Inherits System.Windows.Forms.Form
Dim gVariant As Guid
Dim gsUID As Guid
Dim TheDatabase As System.Data.SqlClient.SqlConnection
Dim CurrentUser As Guid
Dim gApprover As Boolean
Dim gAdministrator As Boolean
Private demoThread As Thread = Nothing
Private childform As frmAdjInfo
Dim da2 As SqlDataAdapter
Dim ds As DataSet = New DataSet
Public Sub SetReceive(ByVal Value As Guid, ByVal Approver As Boolean, ByVal Administrator As Boolean)
CurrentUser = Value
gApprover = Approver
gAdministrator = Administrator
End Sub
Function LaunchForm() As Integer
Call TestThread()
LaunchForm = Nothing
End Function
Private Sub TestThread()
Me.demoThread = New Thread(New ThreadStart(AddressOf Me.ThreadProc))
Me.demoThread.SetApartmentState(ApartmentState.STA)
Me.demoThread.Start()
End Sub
Private Sub ThreadProc()
frmNewAdjustment.SetReceive(CurrentUser)
frmNewAdjustment.Show()
frmNewAdjustment.Hide()
End Sub
Private Sub frmAdjustments_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call LaunchForm()
If gApprover = False Then
tsApprove.Enabled = False
tsReject.Enabled = False
End If
If gAdministrator = False Then
tsAdmin.Enabled = False
End If
Call PopulateData()
End Sub
Function PopulateData() As Integer
Dim ConnectionString As String
Dim strSQLBaseDataset As String
Dim da As New SqlDataAdapter
Dim TheDatabase2 As System.Data.SqlClient.SqlConnection
DataGridView1.Refresh()
ConnectionString = "Data Source=;Initial Catalog=Adjustments;User ID=;Password="
TheDatabase2 = New SqlClient.SqlConnection(ConnectionString)
strSQLBaseDataset = "SELECT tAdjustmentHeader.Id, CAST(YEAR(tAdjustmentHeader.AdjustmentPeriod) as CHAR(4)) + '-' + DATENAME(MONTH,tAdjustmentHeader.AdjustmentPeriod) as [Summary Month], " & _
"tUser.FullName AS [Created By], tAdjustmentHeader.PostedDateTime as Posted, tAdjustmentHeader.Approved, tAdjustmentHeader.TypeCode, tAdjustmentHeader.OrderID, tAdjustmentHeader.BTN, " & _
"tAdjustmentHeader.Description, tAdjustmentHeader.Resolution FROM tUser INNER JOIN " & _
"tAdjustmentHeader ON tUser.Id = tAdjustmentHeader.OwnerId WHERE tAdjustmentHeader.DeletedDateTime IS NULL Order by AdjustmentPeriod DESC,CREATIONDATETIME DESC;"
da.SelectCommand = New SqlCommand()
da.SelectCommand.Connection = TheDatabase2
da.SelectCommand.CommandText = strSQLBaseDataset
da.SelectCommand.CommandType = CommandType.Text
da.Fill(ds, "TestAdjustments")
Dim objDataView = New DataView(ds.Tables("TestAdjustments"))
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = ds
DataGridView1.DataMember = "TestAdjustments"
With Me.DataGridView1
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.Columns("ID").Visible = False
End With
TheDatabase2.Close()
da = Nothing
PopulateData = Nothing
End Function
End Class
I am basically passing a GUID using the a SetReceive property on the NewAdjustments form. When I launch this form I'm getting an error because it's not passing the setreceive value which is the GUID and mandatory to identify the user making the change to the record.
I've been wrestling with adding threading to my app unsuccessfully for a few days and could use any help from this forum.
Thanks in advance for your replies.
Frustated!!