views:

22

answers:

2

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
+1  A: 

Because you don't describe what "not working correctly" means, I can't provide an actual solution. I can, however, give some advice:

  1. If one of your processes is interactive, that doesn't seem like a good candidate for an asynchronous operation in a console application. While a WinForms or WPF application could provide a dedicated form for interacting with this process, your console window can't provide the same sort of isolation.
  2. Is there a reason you're using an STA thread for operation 1?
  3. Also for operation 1, why are you setting IsBackground to true? Background threads will not prevent the process from exiting, so if the process exits while the thread is in progress, it will simply kill the thread without waiting for it to complete.
Adam Robinson
Sorry for lack of clarity...I set the thread 1 to STA because I was getting an error when using the openfiledialog on a thread other than the main thread. I added background is true since someone else suggested that on another forum. When I run the prgm and select 1, the process does not call the openfiledialog which is in the loadfile process but rather calls the menu again with Enter a choice;I select choice 2 perform the functions it has and after its complete the process ends and nothing happens...prgm freezes
vbNewbie
also I will be adding forms later but for right now I am required to have the core working first
vbNewbie
@vbNewbie: That explains it; if you're using a Windows forms control, it has to be on an STA thread. Perhaps posting the code for your two thread functions would be helpful. As for having the "core working first", it's not a common (or easy) path to move from console to windows forms. It's much easier to start with a very basic Windows Forms application that doesn't do much and build upon that rather than trying to implement the state-machine architecture of a console application then moving to the event-based architecture of a Windows forms application.
Adam Robinson
+1  A: 

The Join method waits for a thread to complete. This means that it doesn't matter which menu item has been selected, it always waits until it has completed before you get to select another menu option.

The "IsBackground" property just tells the operating system that if the main thread is closed, background threads are closed automatically.

Here's an idea:

1) create a boolean variable MyThreadRunning

2) if MyThreadRunning = true, don't show the menu option for that thread

3) inside the thread's method, when it's finished working, set the MyThreadRunning to false

4) when you select a menu option that starts a thread, set the MyThreadRunning variable to true and start the thread.

Of course you need different variables (or an array) for multiple threads. And use the volatile keyword on the variable, or a locking mechanism.

EDIT: I really need to watch the preview before posting :(

deltreme
Thanks so much for your response, gonna give it a shot
vbNewbie
could you post a sample way of doing this. Not sure how this helps the thread not stall.
vbNewbie