Fase

Doesn't seem the same way as threading with a Form.
I basically create a thread, and want it to return a value to a sub in my main module

Ex. Convert this to work with a console

Private MainForm As Form = frmMain
Private Delegate Sub dReceive(ByVal clientdata As String)
Private mReceive As dReceive = AddressOf frmMain.ReceiveMessage

'Thread Invoke
Public Sub Invoke(ByVal Args() As Object)
MainForm .Invoke(mReceive, Args)
End Sub



Re: Visual Basic Express Edition Threading With a Console Window

element109

Code Snippet

Module Module1

Dim WithEvents bw As New ComponentModel.BackgroundWorker

Sub Main()

Dim Args() As Object = Environment.GetCommandLineArgs

bw.RunWorkerAsync(Args)

Console.ReadLine()

End Sub

Public Function Invoke(ByVal Args() As Object) As String

Dim clientdata As String = CStr(Args(0))

Return clientdata

End Function

Private Sub bw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork

e.Result = Invoke(CType(e.Argument, [Object]()))

End Sub

Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted

Console.WriteLine(e.Result)

End Sub

End Module





Re: Visual Basic Express Edition Threading With a Console Window

Fase

The thread I'm running must send messages back while it's running, not when it's done.




Re: Visual Basic Express Edition Threading With a Console Window

Fase

Turns out unlike forms, I don't need to pass messages back to the original thread. I can have console.write in any of my threads and it's output.




Re: Visual Basic Express Edition Threading With a Console Window

Fase

Still want to be able to have my thread tell my main thread to run a sub. So we have my console window that booted, it starts a thread. That thread sometime sends a message back to my console that says "run this sub".




Re: Visual Basic Express Edition Threading With a Console Window

element109

Enable the background worker's reportprogress property and add the report progress event handler.

Code Snippet

Me.BackgroundWorker1.WorkerReportsProgress = True

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

'Call your sub on the main thread here.

End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Me.BackgroundWorker1.ReportProgress(, )

End Sub





Re: Visual Basic Express Edition Threading With a Console Window

nobugz

You might be a bit hung-up on how Windows Forms work. You need to use Control.Invoke() or BackgroundWorker.ReportProgress() for it but there's no such need for a console mode app. Just have your second thread call the sub instead of asking the first thread to run it. You'll need to use the SyncLock statement to prevent the threads stepping on each others toes.