[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?
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:
dThread.IsBackground = True
by the way you are doing this is not the best!!
Re: Multithreading in console app
Quote:
by the way you are doing this is not the best!!
I'm all ears.
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.
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.
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
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 . . .
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