vb.net threading

G

Guest

I have been working on a project that needing a separate thread....and have
finally worked out a solution (I think). I had made many attempts to use
events and such but to no avail. Then I noticed an MSDN article called
"Safe, Simple Multithreading in Windows Forms, Part1 and Part2". This helped
tremendously... I believe my previous attempts did not work because I was
using my User Interface object to create the worker thread directly...and the
article more or less indicates that this isn't a good idea. Or at least
shutting down the worker thread from the UI isn't good. So that is the
reason for the thread to start the worker thread below. Does the below look
ok? Any suggestions?
Thanks,
-Kenny
Public Class Main
Private Shared dblocation As String
Private Shared exlog As New ExceptionLog

Shared Sub main()
dblocation = GetSetting(Application.ProductName, "Default", "dbPath")
If IsValidDBConnection() Then
'good connection fire up user interface
Dim ui As New Form1(dblocation)
ui.ShowDialog()
Else
Dim defineDB As New frmDBDefinition
defineDB.ShowDialog()
End If
End Sub
Private Share Function IsValidDBConnection() as Boolean
End Class

Public Calss Form1
Private threadMod as New ThreadModerator
Private processThread as New Thread(Addressof threadMod.Start)

'bunch of stuff related to the UI
'...
Private Sub btnSubmit_Click()
'do some stuff
'call threading process
GoThreading()
end Sub
Private Sub GoThreading()
'Ask the moderating thread if the worker thread is running...
'If so then no need to fire up another thread to call the moderator...
'The worker thread is polling a database for a status change and my
'preference is to have only one worker thread hitting the db at any
given time

If threadMod.IsRunning = False then
threadMod.IsRunning = True
processThread = New Thread(AddressOf threadMod.Start)
processThread.Start()
end If
end sub
Private Sub Form1_Closing()
'if the worker is still working then do allow the application to be
closed.
Try
If threadMod.IsRunning = True Then
Messagebox.show("Termination Prevented...Worker Process Still
Running..")
e.Cancel = True
End If
Catch ex as Exception
end try
End Sub
End Class

Public Delegate Function AsyncDelegate(<Out> Byref threadID as Integer) As
Boolean
Public Class ThreadModerator
Shared StartServer as Boolean = True
Shared dblocation as string
Property IsRunning() as boolean
'get /set StartServer
End Property
Property Setdblocation as string
'get/Set dblocation
End Property
Shared Sub Start()
'if the process is running...no need to start another
if StartServer = true then
Dim threadID as Integer
Dim myTask as New PollingDBTask
myTask.SetDblocation = dblocation
Dim wkrDelegate as New AsyncDelegate(Adderssof myTask.GoProcess)
Dim threadResult as IAsynResult = wkrDelegagte.BeginInvoke(threadID,
Nothing, Nothing)
StartServer = wkrDelegate.EndInvoke(threadID, threadResult)
End if
End Sub
End Class

Public Class PollingDBTask
Public Function GoProcess(<Out> byref threadID as integer) as boolean
'run my long running db task
while Not done
PollDB_And_Process()
End While
'I'm done, no more db processing to perform
'return false to shut down the threadModerator worker thread
return false
end Function
End Class
 
G

Guest

Almost forgot the other article the was even more help was "Asynchronous
Progrramming Overview"....from the .Net Developer's Guide...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top