Results 1 to 8 of 8

Thread: [RESOLVED] Multithreading in console app

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Resolved [RESOLVED] Multithreading in console app

    Code:
    Sub Main()
    
        Try
            'process i files on their own thread
            Dim iOperation As New Threading.ThreadStart(AddressOf ProcessI)
            Dim iThread As New Threading.Thread(iOperation)
            iThread.Start()
    
            'process d files on their own thread
            Dim dOperation As New Threading.ThreadStart(AddressOf ProcessD)
            Dim dThread As New Threading.Thread(dOperation)
            dThread.Start()
    
            'process p files on their own thread
            Dim pOperation As New Threading.ThreadStart(AddressOf ProcessP)
            Dim profThread As New Threading.Thread(pOperation)
            pThread.Start()
    
            'ensure all threads have finshished working
            'if you don't do this the program will terminate before the work is done
            iThread.Join()
            dThread.Join()
            pThread.Join()
    
        Catch ex As Exception
            'exceptions from worker threads will not bubble up, and will cause the application to hang
            Console.WriteLine(ex.ToString)
        End Try
    
    End Sub
    I'm running into a problem when I stop the debugger, the console window stays open, and I can't kill it via task manager. Any ideas?
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  2. #2
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Multithreading in console app

    set the threads isbackground property to true, this will enable the threads to abort as soon as the app that started it has aborted

    vb Code:
    1. dThread.IsBackground = True

    by the way you are doing this is not the best!!
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  3. #3

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Multithreading in console app

    by the way you are doing this is not the best!!
    I'm all ears.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  4. #4
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Multithreading in console app

    first, explain what you are trying to achieve, then i can suggest a better way.
    why i said it might not be better is the way you are using threads.
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  5. #5

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Multithreading in console app

    Each thread parses/modifies a file. There are three different formats I handle, so I made a thread per format.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Multithreading in console app

    This works

    Code:
        Sub Main()
            Try
                Dim it As New Threading.Thread(AddressOf iFiles)
                it.IsBackground = True
                Dim dt As New Threading.Thread(AddressOf dFiles)
                dt.IsBackground = True
                Dim pt As New Threading.Thread(AddressOf pFiles)
                pt.IsBackground = True
    
                it.Start()
                dt.Start()
                pt.Start()
    
                it.Join()
                dt.Join()
                pt.Join()
    
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub iFiles()
            Console.WriteLine("I")
            Threading.Thread.Sleep(4000)
        End Sub
    
        Private Sub dFiles()
            Console.WriteLine("D")
            Threading.Thread.Sleep(4000)
        End Sub
    
        Private Sub pFiles()
            Console.WriteLine("P")
            Threading.Thread.Sleep(4000)
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Multithreading in console app

    Still having some problems. I was debugging in the i thread, stopped on a breakpoint in a catch block, I stopped the debugger, but my console window stayed open. Time to reboot again . . .
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Multithreading in console app

    Apparently this is an old, yet unresolved issue, that MS is aware of.

    http://blogs.msdn.com/b/debugger/arc...dex=1#comments
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width