473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

vb.net threading

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
Nov 22 '05 #1
1 1230
Almost forgot the other article the was even more help was "Asynchronous
Progrramming Overview"....from the .Net Developer's Guide...
Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
2
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
11
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
0
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.