VB.NET Background Process

  • Thread starter Thread starter Derek Martin
  • Start date Start date
D

Derek Martin

Howdy folks, I have a sleeping thread that runs every 20 seconds or so to
check for messages from a database message store.
When it detects an un-acknowledged message, I am firing off a notification
routine that raises a toaster popup.

The problem is that since it is in a thread, the code keeps going right
through that event and it doesn't allow the app to spawn the alert window
(see attached code) and I end up with a little piece of the alert window and
then it just craps out during the next iteration.

Can someone help fix this or provide a better method of having this sleeper
thread go without interupting the normal program execution?

Thanks!
Derek

My (I know it's ugly cause I am just trying to see if it can get it to work)
code:

'On the form load
u = New Thread(AddressOf message_sleeper)
u.Start()
....
end sub

Private Sub message_sleeper()
Dim i As Integer = 0
Do While True
process_client_messages()
Thread.CurrentThread.Sleep(15000)
Loop
End Sub

Private Function process_client_messages()
Dim cmd As New SqlCommand
Dim intRowsAff1 As Integer
Dim SQLStr1 As String
Dim dr As SqlDataReader
Dim messages As New ArrayList
Dim thismessage As String
Dim thisguid As String
'Get unacknowledged messages
cmd.CommandTimeout = 60
cmd.Connection = Global.conn
cmd.CommandType() = CommandType.Text
cmd.CommandText = "Select id, message from client_messages WHERE
hostname = '" + myhostname.ToString + "' AND ack = '0'" 'myhostname globally
defined
Try
Global.conn.Open()
dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
While dr.Read
thismessage = dr(1).ToString
messages.Add(thismessage.ToString)
End While
Catch ex As Exception
Dim oopsy As New ErrorHandler("Client message processor Get
Messages: ", ex.Message, ex.StackTrace, ex.GetType.ToString)
Dim newmessage As New MessageProcessor
newmessage.process_message(oopsy.HelpLink.ToString)
Finally
Global.conn.Close()
End Try

'Display the messages via alert if it is not a CMD -->This is what gets
stuck cause the thread just blows right past it :-(
Dim i As Integer = 0
For i = 0 To messages.Count - 1
Dim newmessage As New MessageProcessor
newmessage.process_message(messages(i).ToString)
Next
End Function


Thank you!!!!!!
 
Hi,
I had a situation slightly similar to this. The way i solved it was to
1. create a new class and add raiseevents for the events that you want to
create popups for
2. make sure that the class that starts the thread has a withevents for the
class/
3. add handlers to the form that you are calling the thread from to create
the actual popup

Hope this helps.
 
Thanks for your message - I thought that was what I was doing!? Can you
give me a code snippet to illustrate the difference in what I am doing vs.
your solution?

Thanks!
Derek
 
Back
Top