using threads in menu options
- by vbNewbie
I have an app that has a console menu with 2/3 selections. One process involves uploading a file and performing a lengthy search process on its contents, whilst another process involves SQL queries and is an interactive process with the user. I wish to use threads to allow one process to run and the menu to offer the option for the second process to run. However you cannot run the first process twice.
I have created threads and corrected some compilation errors but the threading options are not working correctly. Any help appreciated.
main...
Dim tm As Thread = New Thread(AddressOf loadFile)
Dim ts As Thread = New Thread(AddressOf reports)
....
While Not response.Equals("3")
Try
Console.Write("Enter choice: ")
response = Console.ReadLine()
Console.WriteLine()
If response.Equals("1") Then
Console.WriteLine("Thread 1 doing work")
tm.SetApartmentState(ApartmentState.STA)
tm.IsBackground = True
tm.Start()
response = String.Empty
ElseIf response.Equals("2") Then
Console.WriteLine("Starting a second Thread")
ts.Start()
response = String.Empty
End If
ts.Join()
tm.Join()
Catch ex As Exception
errormessage = ex.Message
End Try
End While
I realize that a form based will be easier to implement with perhaps just calling different forms to handle the processes.But I really dont have that option now since the console app will be added to api later. But here are my two processes from the menu functions. Also not sure what to do with the boolean variabel again as suggested below.
Private Sub LoadFile()
Dim dialog As New OpenFileDialog
Dim response1 As String = Nothing
Dim filepath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
dialog.InitialDirectory = filepath
If dialog.ShowDialog() = DialogResult.OK Then
fileName = dialog.FileName
ElseIf DialogResult.Cancel Then
Exit Sub
End If
Console.ResetColor()
Console.Write("Begin Search -- Discovery Search, y or n? ")
response1 = Console.ReadLine()
If response1 = "y" Then
Search()
ElseIf response1 = "n" Then
Console.Clear()
main()
End If
isRunning = False
End Sub
and the second one
Private Shared Sub report()
Dim rptGen As New SearchBlogDiscovery.rptGeneration
Console.WriteLine("Tread Process started")
rptGen.main()
Console.WriteLine("Thread Process ended")
isRunning = False
End Sub