Multithreaded application not compiling.

Hey just curious if anyone could tell me why my multithreaded application will not work, its my first attempt at coding a multithreaded application, everything in the program works except the starting of the thread where I get the error "Too many arguements to 'Public Sub New()' " which I get when declaring my new thread which would be this code:

Connect = New Thread(AddressOf Thread.Connect)

Anyways heres the entire application, its not overly complicated like I say I just can't figure out why the multithreading is not working.

Imports System.Net.Mail
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System
Imports EnterpriseDT.Util.Debug
Imports EnterpriseDT.Net.Ftp
Imports System.Threading

Public Class Main
Dim disconnect As Integer = 0

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Main As New Main()
Dim Connect
Connect = New Thread(AddressOf Thread.Connect)
Do While disconnect = 0
Connect.Start()
Loop
Connect.Abort()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'FTP TEST VARIABLES
Dim ftpserver As String = ftpserverbox.Text
Dim ftpusername As String = ftpusernamebox.Text
Dim ftppassword As String = ftpusernamebox.Text
Dim ftpclientid As String = ftpclientidbox.Text
Dim dbpath As String = dbpathbox.Text
'END FTP TEST VARIAIBLES

'TESTS FTP CONNECTION
Dim ftp As FTPClient = Nothing
ftp = New FTPClient(ftpserver)
ftp.Login(ftpusername, ftppassword)
ftp.ConnectMode = FTPConnectMode.PASV
ftp.TransferType = FTPTransferType.ASCII

Dim tbl() As String = {"tbldatastore.txt", "tblalarmlog.txt", "tblcomments.txt", "tbldatasources.txt", "tblfracreport.txt", "tblfracreportassemblyitems.txt", "tblfracreportcrew.txt", "tblfracreportfuel.txt", "tblfracreportquoteitems.txt", "tblfracreportrentals.txt", "tblfracstages.txt", "tbljobs.txt"}
Dim count As Integer = 0
Try
ftp.MkDir(ftpclientid)
Catch ex As Exception
End Try
For count = 0 To 11
ftp.Put(dbpath & "\" & tbl(count), ftpclientid & "\" & tbl(count))
Next
ftp.Quit()
MsgBox("upload Complete!")

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'TEST CONNECT VARIABLES
Dim dbserver As String = dbserverbox.Text
Dim dbusername As String = dbusernamebox.Text
Dim dbpassword As String = dbpasswordbox.Text
Dim dbpath As String = dbpathbox.Text
'END TEST CONNECT VARIABLES
Dim conn As New SqlConnection()
conn.ConnectionString = "Data Source=" & dbserver & ";Initial Catalog=master;Persist Security Info=True;User ID=" & dbusername & ";Password= " & dbpassword & ";Connection TimeOut=60;"
Try
conn.Open()
Catch ex As SqlException
MsgBox("Failed to Connect")
End Try
MsgBox("Connected Succesfully")
conn.Close()

End Sub
Public Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

disconnect = 1
End Sub

Private Sub StatusStrip1_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles StatusStrip1.HandleCreated
ToolStrip.Text = "Status"
End Sub

End Class

Public Class Thread

Public Shared Sub Connect()
'GLOBAL VARIABLES FOR DATABASE DUMP AND FTP UPLOAD
Dim dbserver As String = Main.dbserverbox.Text
Dim dbusername As String = Main.dbusernamebox.Text
Dim dbpassword As String = Main.dbpasswordbox.Text
Dim dbpath As String = Main.dbpathbox.Text
Dim ftpserver As String = Main.ftpserverbox.Text
Dim ftpusername As String = Main.ftpusernamebox.Text
Dim ftppassword As String = Main.ftpusernamebox.Text
Dim ftpclientid As String = Main.ftpclientidbox.Text

'CONNECTS TO MSSQL AND DUMPS ALL DATABASE DATA TO 'dbpath'

Main.ToolStrip.Text = "Connecting to Database."
Dim conn As New SqlConnection()
conn.ConnectionString = "Data Source=" & dbserver & ";Initial Catalog=master;Persist Security Info=True;User ID=" & dbusername & ";Password= " & dbpassword & ";Connection TimeOut=60;"
Try
conn.Open()
Catch ex As SqlException
MsgBox("Failed to Connect")
Main.ToolStrip.Text = "Failed to Connect."
End Try
If conn.State = 1 Then
Dim time As Integer = 3000
Main.ToolStrip.Text = "Connected to Database. Dumping data."
Try
Dim command As SqlCommand = New SqlCommand("EDRData.dbo.ExportAlldata", conn)
command.CommandTimeout = time
command.CommandType = CommandType.StoredProcedure

command.Parameters.Add("@jobid", "-1")
command.Parameters.Add("@nonlogdata", "1")
command.Parameters.Add("@path", dbpath)
command.Parameters.Add("@user", dbusername)
command.Parameters.Add("@pwd", dbpassword)
command.Parameters.Add("@server", dbserver)
command.ExecuteNonQuery()
conn.Close()
Catch ex As SqlException
MsgBox(ex.Message)
End Try
End If
'END DATABASE DUMP CODE

'UPLOADS ALL DATA FILES IN 'dbpath' TO DATABASE SERVER
Main.ToolStrip.Text = "Connecting to FTP Server"
Dim ftp As FTPClient = Nothing
ftp = New FTPClient(ftpserver)
ftp.Login(ftpusername, ftppassword)
ftp.ConnectMode = FTPConnectMode.PASV
ftp.TransferType = FTPTransferType.ASCII

Main.ToolStrip.Text = "Uploading dumped data."
Dim tbl() As String = {"tbldatastore.txt", "tblalarmlog.txt", "tblcomments.txt", "tbldatasources.txt", "tblfracreport.txt", "tblfracreportassemblyitems.txt", "tblfracreportcrew.txt", "tblfracreportfuel.txt", "tblfracreportquoteitems.txt", "tblfracreportrentals.txt", "tblfracstages.txt", "tbljobs.txt"}
Dim count As Integer = 0
Try
ftp.MkDir(ftpclientid)
Catch ex As Exception
End Try
For count = 0 To 11
ftp.Put(dbpath & "\" & tbl(count), ftpclientid & "\" & tbl(count))
Next
ftp.Quit()
End Sub
End Class

[6787 byte] By [mitchwardrop] at [2007-12-24]
# 1

Connect = New Global.System.Threading.Thread(AddressOf Thread.Connect)

Since you have defined a class Thread in the current namespace, the compiler will bind to this class definition instead of System.Threading.Thread, and since your Thread class doesn't have a constructor that takes one argument, the compiler will (correctly) complain...

Best regards,
Johan Stenberg

MSJohanStenberg at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
Well thank you very much that definitly made it compile, however I have come up with one other problem now that hopefully you or someone else can help me with agian.

I've edited my button click/thread call code a bit to catch exceptions I'll post it at the bottom, however all it does it kick me out of the thread and doesn't execute anything.

THe new connect code

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Main As New Main()
Dim Connect
Connect = New Global.System.Threading.Thread(AddressOf Thread.Connect)
Do While disconnect = 0
Try
Connect.Start()
Catch ex As Exception
MsgBox("thread error")
End Try

Loop
Connect.Abort()

End Sub

mitchwardrop at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

If you happen to use Visual Studio 2005/.NET framework v2.0, I would definately recommend that you take a look at the BackgroundWorker class. You have to be careful about what you touch from which thread, in particular, you can't touch any controls from a thread other than the thread that created the control. In your case, you do read the values from textboxes (at least, given the field names, I assume that they are textboxes) from within your thread. That is a no-no (it may work, or it may just appear to work, or it may work in 99.99% of the cases, which of course means that it'll crash as soon as a customer can see the application, or you are doing a demo for your boss :))

In your application, you also have a problem in that Button1_Click will start the thread, continue with its Do While disconnect = 0 Loop, immediately come back and start the thread again, which will cause a ThreadStateException to be thrown

Best regards,
Johan Stenberg

MSJohanStenberg at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...