开发者

using threads in menu options

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


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.


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 :(

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜