GlobaLevel
asked on
.net threading - run A, if B awakes..run B...at end go back to A
How would I get this sample code I found on EE to run A, if B awakes..run B...at end go back to A?
Imports System.Threading
Public Class Form1
Private mainPicSize As Size
Private threadLockObject As Object
Private Sub generate_A()
Monitor.Enter(threadLockObject)
Try
Dim img1 As New Bitmap(mainPicSize.Width, mainPicSize.Height)
Finally
Monitor.Exit(threadLockObject)
End Try
'...do some other things
End Sub
Private Sub generate_B()
Monitor.Enter(threadLockObject)
Try
Dim img1 As New Bitmap(mainPicSize.Width, mainPicSize.Height)
Finally
Monitor.Exit(threadLockObject)
End Try
'...do some other things
End Sub
Private Sub generate_ALL()
Dim A_thread As New Threading.Thread(AddressOf generate_A)
Dim B_thread As New Threading.Thread(AddressOf generate_B)
A_thread.IsBackground = True
B_thread.IsBackground = True
' ------these two thread need to run contiunally for 6 hours..continually running A
' ------ if B awakes...run B...go back to A at end og B....
' if some process comes thru..kick off A
A_thread.Start()
' if some process comes thru..kick off B
B_thread.Start()
End Sub
End Class
ASKER
you want B to have priority over A . . . running until completion and then let A continue?
--yes
Also I'm new to threading and I don't know what you mean by scheduler...
--yes
Also I'm new to threading and I don't know what you mean by scheduler...
Can you give more details?
How are the threads staying alive?
How do we know when B "awakes"?
Give a better idea of what is happening in A and B.
How are the threads staying alive?
How do we know when B "awakes"?
Give a better idea of what is happening in A and B.
Is there any reason for this to be threaded then? Why can't A do it's thing, check if B is awake, and then call a subfunction. Can all be done in one thread.
Like Idle_Mind said . . . how do we know when B needs to execute?
Like Idle_Mind said . . . how do we know when B needs to execute?
ASKER
yes, on the cell..I have an SMS interceptor..B..when a text comes in...handle it...when doen go back to A....a is sending out SMS...
here is the code for B..recieve SMS..
here is the code for B..recieve SMS..
Public Class Form1
'http://msdn.microsoft.com/en-us/library/bb932385.aspx
Private _smsInterceptor As MessageInterceptor = Nothing
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Default constructor indicates that message handling should occur
' on the main application thread, and message should be passed
' to other message interceptors and to the SMS message reader
Try
_smsInterceptor = New MessageInterceptor()
_smsInterceptor.MessageCondition = New MessageCondition(MessageProperty.Sender, _
MessagePropertyComparisonType.NotEqual, "*", False) ''' CATCH ALL...
' Explicitly attach the event handler rather than use WithEvents on the decleration
AddHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
Catch ex As Exception
MessageBox.Show("SMS_INtercepter Area - ERROR: " & ex.Message)
End Try
End Sub
Private Sub SmsInterceptor_MessageReceived(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)
Try
' Cast to SmsMessage to access message body
' Not expecting to receive any non-SMS messages but use "as" to
' cast to be extra safe
Dim var_to As String
Dim var_message As String
Dim var_from As String
Dim newMessage As SmsMessage = TryCast(e.Message, SmsMessage)
If Not newMessage Is Nothing Then
' StatusBar1.Text = "From:" & newMessage.From.Address
Debug.WriteLine(String.Format("Sender:{0} - Body:{1}", newMessage.From.Address, newMessage.Body))
' IF THE NEW MESSAGE IS A A NEW SMS SEND THE INFO
' TO THE APPLY_DB FUNCTION TO APPLY
' IT TO THE SQL DB FOR STORAGE
var_to = newMessage.To.ToString
var_message = newMessage.Body.ToString()
var_from = newMessage.From.Address
apply_db(var_to, var_message, var_from)
End If
Catch ex As Exception
MessageBox.Show("SMS_Recieved - ERROR: " & ex.Message)
End Try
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
If Not _smsInterceptor Is Nothing Then
RemoveHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
_smsInterceptor.Dispose()
End If
End Sub
Public Sub apply_db(ByVal var_to As String, ByVal var_message As String, ByVal var_from As String)
Try
Dim connectionString As String = "Data Source=xx.xx.xx.xx;Initial Catalog=xxx;Trusted_connection=true;User ID=;Password=;"
' Open the connection using the connection string.
Using connection As New SqlConnection(connectionString)
connection.Open()
' Insert into the SqlCe table. ExecuteNonQuery is best for inserts.
Using com As New SqlCommand("INSERT INTO Message_Queue_Recieve (TO_NUMBER, MESSAGE) Values(@TO_NUMBER,@MESSAGE, @FROM_MESSSAGE)", connection)
com.Parameters.AddWithValue("@TO_NUMBER", var_to)
com.Parameters.AddWithValue("@MESSAGE", var_message)
com.Parameters.AddWithValue("@FROM_MESSAGE", var_from)
com.ExecuteNonQuery()
End Using
connection.Close()
End Using
Catch ex As Exception
MessageBox.Show("SQL Insert - ERROR: " & ex.Message)
End Try
End Sub
End Class
Ok...I'm assuming that ThreadA is NOT in the main UI thread and that it also has some kind of infinite looping condition (like a "while true").
Hopefully this example will illustrate how it can be done using SyncLock statements:
Hopefully this example will illustrate how it can be done using SyncLock statements:
Public Class Form1
Public SyncObject As New Object
Private Sub ThreadA()
While True
' ... some code ...
' if ThreadB() currently has "SyncObject" locked then ThreadA() will HALT below until ThreadB() releases it
SyncLock SyncObject
' ... send off an SMS ...
' If ThreadB() fires while we are in here, then ThreadB() will HALT until ThreadA() exits the synclock
' block and releases "Sync
End SyncLock
' ... some more code ...
' When outside the synclock, we have released "SyncObject" allowing some other thread a chance to lock it
' here I'm using a while loop but it could also be some kind of for loop as well.
End While
End Sub
Private Sub ThreadB() ' <-- triggered by an event
' we will halt below until ThreadA() release its lock on "SyncObject"
SyncLock SyncObject
' ... when we get in here, ThreadA() will be PAUSED at its SyncLock statement until we leave the SyncLock block here ...
End SyncLock
End Sub
End Class
ASKER
Well ..here is the whole thing...again you see the loop...this code..just jams the phone up..and I have to shut it down..and restart...ineffectve...
ASKER
code..
import System
import System.Linq
import System.Collections.Generic
import System.ComponentModel
import System.Data
import System.Drawing
import System.Text
import System.Windows.Forms
import Microsoft.WindowsMobile.PocketOutlook
Public Class Form1
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.click
' KICK OFF THE WHOLE PROCESS...LOOP INDEFINITELY...
For x = 1 To 700000
' IF A SMS MSG COMES IN DEAL WITH IT...OTHER WISE..KEEP SENDING SMS..
recieve_SMS(sender, e) ' B THREAD...
System.Threading.Thread.Sleep(30) ' LET THE SYSTEM REFRESH....DO EVENTS...
outgo_sms_db() ' A THREAD...
Next
End Sub
'http://msdn.microsoft.com/en-us/library/bb932385.aspx
Private _smsInterceptor As MessageInterceptor = Nothing
Private Sub recieve_SMS(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles MyBase.Load
' Default constructor indicates that message handling should occur
' on the main application thread, and message should be passed
' to other message interceptors and to the SMS message reader
' LOOP CONTINUALLY TO CATCH ALL INCOMING SMS....
Do Until _smsInterceptor = ""
_smsInterceptor = New MessageInterceptor()
_smsInterceptor.MessageCondition = New MessageCondition(MessageProperty.Sender, _
MessagePropertyComparisonType.Contains, "*", False) ''' CATCH ALL...
' Explicitly attach the event handler rather than use WithEvents on the decleration
AddHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
' okay so it got a inbound sms ..now switch up ..and send a sms from db..and trade off and on
'between send and recieve...
outgo_sms_db()
Loop
End Sub
Private Sub SmsInterceptor_MessageReceived(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)
' Cast to SmsMessage to access message body
' Not expecting to receive any non-SMS messages but use "as" to
' cast to be extra safe
Dim var_to As String
Dim var_message As String
Dim var_from As String
Dim newMessage As SmsMessage = TryCast(e.Message, SmsMessage)
If Not newMessage Is Nothing Then
StatusBar1.Text = "From:" & newMessage.From.Address
Debug.WriteLine(String.Format("Sender:{0} - Body:{1}", newMessage.From.Address, newMessage.Body))
' IF THE NEW MESSAGE IS A A NEW SMS SEND THE INFO
' TO THE APPLY_DB FUNCTION TO APPLY
' IT TO THE SQL DB FOR STORAGE
var_to = newMessage.To.Address
var_message = newMessage.Message.Address
var_from = newMessage.From.Address
apply_db(var_to, var_message, var_from)
End If
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
If Not _smsInterceptor Is Nothing Then
RemoveHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
_smsInterceptor.Dispose()
End If
End Sub
Public Sub apply_db(ByVal var_to As String, ByVal var_message As String, ByVal var_from As String)
Dim SQLConn1 As SqlConnection = New SqlConnection()
Dim cmd1 As SqlCommand
Dim intRowsAff1 As Integer
Dim SQLStr1 As String
Dim Err1 As String
SQLConn1.ConnectionString = "Network Library=DBMSSOCN; Data Source=Server Address,server port; Initial Catalog=database name; User ID=username; Password=password"
SQLStr1 = ("INSERT INTO message_incoming (TO_NUMBER, MESSAGE, FROM_NUMBER) " & _
"Values('" & var_to & "', '" & var_message & "', '" & var_from & "')")
SQLConn1.open()
cmd1 = New SQLCommand(SQLStr1, SQLConn1)
Try
Err1 = cmd1.ExecuteNonQuery()
Finally
SQLConn1.Close()
End Try
End Sub
Public Sub outgo_sms_db()
InitializeComponent()
Dim var_from As String
Dim var_to As String
Dim var_message As String
Dim var_body As String
Dim x As String
x = 0
string sqlConnection As String
Using sqlConn As New SqlConnection
sqlConnection = "Data Source=xx.xx.xx.xxInitial Catalog=WxxfgITTrusted_connection=true"
sqlConn.ConnectionString = sqlConnection
Dim SQLstring As String = "SELECT * FROM PRODUCT p Join customer c ON c.id = p.id WHERE WxxD = @var_xxIxt_ID AND 1 = 1"
Using command As New SqlCommand(SQLstring, sqlConn)
command.Parameters.Add(New SqlParameter("@var_xxIxt_ID", SqlDbType.Int)).Value = var_xxIxT_ID
dataReader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
If dataReader.HasRows Then
Do While dataReader.Read()
FROM = dataReader("FROM").ToString
TO_PERSON = dataReader("TO_PERSON").ToString
SUBJECT = dataReader("SUBJECT").ToString
BODY = dataReader("BODY").ToString
var_from = From
var_to = TO_PERSON
var_message = SUBJECT
var_body = body
Send(var_to, var_message)
Loop
sqlConn.close()
Else
End If
End Using
End Using
' send sms via windows handler...
' see this link for help
' http:'msdn.microsoft.com/en-us/library/aa446561.aspx
End Sub
public sub Send(string to, string msg)
if ((to <> string.Empty) && (msg <> string.Empty))
SmsMessage message = new SmsMessage(to, msg)
try
message.Send()
catch (Exception ex)
MessageBox.Show(ex.ToString())
End Sub
End Class
...*I'm definitely NOT a phone developer*....
Lines #39 thru #51 definitely throw up red flags in my mind. You would normally never be repeatedly creating an object and using AddHandler() on it while in a loop like that! From the MSDN article in the comments, it shows MessageInterceptor() being created ONCE at the beginning of the app.
You have loops inside the methods being called as well. It's unclear to me, but basically any loops running in the main UI thread need to call Application.DoEvents() to keep the UI responsive. Any tight loops in other threads may need to be throttle with Sleep().
The overall design of the app may need to be redone... =\
Lines #39 thru #51 definitely throw up red flags in my mind. You would normally never be repeatedly creating an object and using AddHandler() on it while in a loop like that! From the MSDN article in the comments, it shows MessageInterceptor() being created ONCE at the beginning of the app.
You have loops inside the methods being called as well. It's unclear to me, but basically any loops running in the main UI thread need to call Application.DoEvents() to keep the UI responsive. Any tight loops in other threads may need to be throttle with Sleep().
The overall design of the app may need to be redone... =\
ASKER
The overall design of the app may need to be redone... =\
....yes I agree...some of the issues..im having..
windows moble 6 - SMS interceptor cant always remain in scope...Question: Im programming a WM 6 HTC touch Pro...and using this link for the code to write the SMS interceptor code...
http://msdn.microsoft.com/en-us/library/bb932385.aspx
in that article...it says that the MessageInterceptor class must remain in-scope otherwise it may stop monitoring....which is the case..it stops monitoring..the problem is...is that it needs to trade off with another process..once the sms interceptor class gets focus...it wont give it up or craps out...any ideas how I can get around this?
....yes I agree...some of the issues..im having..
windows moble 6 - SMS interceptor cant always remain in scope...Question: Im programming a WM 6 HTC touch Pro...and using this link for the code to write the SMS interceptor code...
http://msdn.microsoft.com/en-us/library/bb932385.aspx
in that article...it says that the MessageInterceptor class must remain in-scope otherwise it may stop monitoring....which is the case..it stops monitoring..the problem is...is that it needs to trade off with another process..once the sms interceptor class gets focus...it wont give it up or craps out...any ideas how I can get around this?
The note at that link states:
Translation: declare it at the class level...not in a sub/function as a local.
This doesn't mean you have to continually create a new instance for it to work...
So you already have it declared at class level with line #31.
Lines #41 thru #45 should only be called ONCE for your app...use the Load() event of the form:
Like the SystemState class, the MessageInterceptor class must remain in-scope otherwise it may stop monitoring. For this reason, you rarely, if ever, create an instance of the MessageInterceptor class as a local variable. Instead, you'll almost always create instances of the MessageInterceptor class as a class-level field
Translation: declare it at the class level...not in a sub/function as a local.
This doesn't mean you have to continually create a new instance for it to work...
So you already have it declared at class level with line #31.
Lines #41 thru #45 should only be called ONCE for your app...use the Load() event of the form:
_smsInterceptor = New MessageInterceptor()
_smsInterceptor.MessageCondition = New MessageCondition(MessageProperty.Sender, _
MessagePropertyComparisonType.Contains, "*", False) ''' CATCH ALL...
' Explicitly attach the event handler rather than use WithEvents on the decleration
AddHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
ASKER
Translation: declare it at the class level...not in a sub/function as a local.
>>...I went back and took a look at that link...I found it confusing that they wanted it at class level..when in there example, they had it at function level...am I missing something here..also..I added a part...if the message is empty jump to send_SMS...see revised...
>>...I went back and took a look at that link...I found it confusing that they wanted it at class level..when in there example, they had it at function level...am I missing something here..also..I added a part...if the message is empty jump to send_SMS...see revised...
import System
import System.Linq
import System.Collections.Generic
import System.ComponentModel
import System.Data
import System.Drawing
import System.Text
import System.Windows.Forms
import Microsoft.WindowsMobile.PocketOutlook
Public Class Form1
' Do not use WithEvents or cannot associate a MessageCondition
Private _smsInterceptor As MessageInterceptor = Nothing
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Default constructor indicates that message handling should occur
' on the main application thread, and message should be passed
' to other message interceptors and to the SMS message reader
_smsInterceptor = New MessageInterceptor()
_smsInterceptor.MessageCondition = New MessageCondition(MessageProperty.Sender, _
MessagePropertyComparisonType.NotContains, "*", False)
' Explicitly attach the event handler rather than use WithEvents on the decleration
AddHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
End Sub
Private Sub SmsInterceptor_MessageReceived(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)
' Cast to SmsMessage to access message body
' Not expecting to receive any non-SMS messages but use "as" to
' cast to be extra safe
Dim var_to As String
Dim var_message As String
Dim var_from As String
Dim newMessage As SmsMessage = TryCast(e.Message, SmsMessage)
If Not newMessage Is Nothing Then
StatusBar1.Text = "From:" & newMessage.From.Address
Debug.WriteLine(String.Format("Sender:{0} - Body:{1}", newMessage.From.Address, newMessage.Body))
' IF THE NEW MESSAGE IS A A NEW SMS SEND THE INFO
' TO THE APPLY_DB FUNCTION TO APPLY
' IT TO THE SQL DB FOR STORAGE
var_to = newMessage.To.Address
var_message = newMessage.Message.Address
var_from = newMessage.From.Address
apply_db(var_to, var_message, var_from)
End If
' NO MESSAGE IS COMING IN...THERE FOR DO THE A THREAD...SEND SMS...
If newMessage Is Nothing Then
outgo_sms_db() ' A THREAD...
End If
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
If Not _smsInterceptor Is Nothing Then
RemoveHandler _smsInterceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived
_smsInterceptor.Dispose()
End If
End Sub
Public Sub apply_db(ByVal var_to As String, ByVal var_message As String, ByVal var_from As String)
Dim SQLConn1 As SqlConnection = New SqlConnection()
Dim cmd1 As SqlCommand
Dim intRowsAff1 As Integer
Dim SQLStr1 As String
Dim Err1 As String
SQLConn1.ConnectionString = "Network Library=DBMSSOCN; Data Source=Server Address,server port; Initial Catalog=database name; User ID=username; Password=password"
SQLStr1 = ("INSERT INTO message_incoming (TO_NUMBER, MESSAGE, FROM_NUMBER) " & _
"Values('" & var_to & "', '" & var_message & "', '" & var_from & "')")
SQLConn1.open()
cmd1 = New SQLCommand(SQLStr1, SQLConn1)
Try
Err1 = cmd1.ExecuteNonQuery()
Finally
SQLConn1.Close()
End Try
End Sub
Public Sub outgo_sms_db()
InitializeComponent()
Dim var_from As String
Dim var_to As String
Dim var_message As String
Dim var_body As String
Dim x As String
x = 0
string sqlConnection As String
Using sqlConn As New SqlConnection
sqlConnection = "Data Source=xx.xx.xx.xxInitial Catalog=WxxfgITTrusted_connection=true"
sqlConn.ConnectionString = sqlConnection
Dim SQLstring As String = "SELECT * FROM PRODUCT p Join customer c ON c.id = p.id WHERE WxxD = @var_xxIxt_ID AND 1 = 1"
Using command As New SqlCommand(SQLstring, sqlConn)
command.Parameters.Add(New SqlParameter("@var_xxIxt_ID", SqlDbType.Int)).Value = var_xxIxT_ID
dataReader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
If dataReader.HasRows Then
Do While dataReader.Read()
FROM = dataReader("FROM").ToString
TO_PERSON = dataReader("TO_PERSON").ToString
SUBJECT = dataReader("SUBJECT").ToString
BODY = dataReader("BODY").ToString
var_from = From
var_to = TO_PERSON
var_message = SUBJECT
var_body = body
Send(var_to, var_message)
Loop
sqlConn.close()
Else
End If
End Using
End Using
' send sms via windows handler...
' see this link for help
' http:'msdn.microsoft.com/en-us/library/aa446561.aspx
End Sub
public sub Send(string to, string msg)
if ((to <> string.Empty) && (msg <> string.Empty))
SmsMessage message = new SmsMessage(to, msg)
try
message.Send()
catch (Exception ex)
MessageBox.Show(ex.ToString())
End Sub
End Class
No..the MSDN examples all have it declared at class level... =)
"I added a part...if the message is empty jump to send_SMS"
In this part of your code:
If newMessage Is Nothing Then
outgo_sms_db() ' A THREAD...
End If
Is it even possible for that condition to occur? Why would you get a MessageReceived() event with no message?
How is outgo_sms_db() being run on a different thread? Are you not showing some of the code?...
"I added a part...if the message is empty jump to send_SMS"
In this part of your code:
If newMessage Is Nothing Then
outgo_sms_db() ' A THREAD...
End If
Is it even possible for that condition to occur? Why would you get a MessageReceived() event with no message?
How is outgo_sms_db() being run on a different thread? Are you not showing some of the code?...
ASKER
Is it even possible for that condition to occur? Why would you get a MessageReceived() event with no message?
--I'm not sure was going to try later and see what happens
This is all the code...instead of threading was relying on the
Newmessage being empty...it checks the first condition why couldn't this work ...was my thinking..,
--I'm not sure was going to try later and see what happens
This is all the code...instead of threading was relying on the
Newmessage being empty...it checks the first condition why couldn't this work ...was my thinking..,
From my understanding you'll only get that event when an actual SMS comes in. It won't be firing repeatedly with no message...
Hi,
Looking at the smaller example here http:#35219283 for example on line 51 when you get a SMS you add the entry to the database. Basically in coding terms I get the following impressions.
For each incoming SMS open the database connection and write a single entry then close database all from the same thread/event. How long does that take? Doesn't that tie up your event handler. Then you have similiar code that does the same but for reading from the database in the same thread/event.
Use some type of cache instead and only commit to the database when the cache reaches a specific max value that way you can do all your database work from a different thread and just work with the SMS stuff from the same thread.
1) The SMS recieved on main thread and commit the information into cache of each Message()
2) Commit a thread to send SMS.
3) Commit a thread to backup your cache to database.
Questions
1) How long does it take to send one SMS message?
2) How long does it take to commit one entry into your database?
3) How many SMS are incoming while all that is happening.
Just some things that might help you layout your code better. If you find commiting a single entry to your database and sending a single SMS takes alot of time then you need to seperate these two routines from the same thread/event of your reciever.
Looking at the smaller example here http:#35219283 for example on line 51 when you get a SMS you add the entry to the database. Basically in coding terms I get the following impressions.
For each incoming SMS open the database connection and write a single entry then close database all from the same thread/event. How long does that take? Doesn't that tie up your event handler. Then you have similiar code that does the same but for reading from the database in the same thread/event.
Use some type of cache instead and only commit to the database when the cache reaches a specific max value that way you can do all your database work from a different thread and just work with the SMS stuff from the same thread.
1) The SMS recieved on main thread and commit the information into cache of each Message()
2) Commit a thread to send SMS.
3) Commit a thread to backup your cache to database.
Questions
1) How long does it take to send one SMS message?
2) How long does it take to commit one entry into your database?
3) How many SMS are incoming while all that is happening.
Just some things that might help you layout your code better. If you find commiting a single entry to your database and sending a single SMS takes alot of time then you need to seperate these two routines from the same thread/event of your reciever.
ASKER
MedievelWarrior...
thanks for the points...
usually when it commits the data to the db...its seconds at the most..ussually pretty fast...if this helps...Im new to multithreading ...hence the post...
Idle_Mind...
From my understanding you'll only get that event when an actual SMS comes in. It won't be firing repeatedly with no message...
...but if the SMS interceptor looses focus..then it stops montioring...if its embedded in a thread..wont it loose focus??..thus negating the asset of multthreading in this situation.
...fyi...
in order to keep the SMS Send alive...I embedded the message in a loop...yet this isnt working...im still not sure how to put that into a class...it didnt work for me...
Private Sub SmsInterceptor_MessageRece ived(ByVal sender As Object, ByVal e As MessageInterceptorEventArg s)
Try
' Cast to SmsMessage to access message body
' Not expecting to receive any non-SMS messages but use "as" to
' cast to be extra safe
Dim var_to As String
Dim var_message As String
Dim var_from As String
Dim newMessage As SmsMessage = TryCast(e.Message, SmsMessage)
' -------------------------- ---------- ---------- ------
'
For x = 1 To 750000
If Not newMessage Is Nothing Then
' StatusBar1.Text = "From:" & newMessage.From.Address
Debug.WriteLine(String.For mat("Sende r:{0} - Body:{1}", newMessage.From.Address, newMessage.Body))
' IF THE NEW MESSAGE IS A A NEW SMS SEND THE INFO
' TO THE APPLY_DB FUNCTION TO APPLY
' IT TO THE SQL DB FOR STORAGE
var_to = newMessage.To.ToString()
var_message = newMessage.Body.ToString()
var_from = newMessage.From.Address()
TextBox1.Text = var_to
TextBox2.Text = var_message
TextBox3.Text = var_from
apply_db(var_message, var_from)
Else
send_sms_main(sender, e)
TextBox5.Text = x
Label4.Visible = True
Label4.Text = "SYS ERR:"
TextBox4.Text = "PRG CMPT. REBOOT APP."
End If
Next
' -------------------------- ---------- ---------- ---------- --------
Catch ex As Exception
Label2.Text = ex.Message
End Try
thanks for the points...
usually when it commits the data to the db...its seconds at the most..ussually pretty fast...if this helps...Im new to multithreading ...hence the post...
Idle_Mind...
From my understanding you'll only get that event when an actual SMS comes in. It won't be firing repeatedly with no message...
...but if the SMS interceptor looses focus..then it stops montioring...if its embedded in a thread..wont it loose focus??..thus negating the asset of multthreading in this situation.
...fyi...
in order to keep the SMS Send alive...I embedded the message in a loop...yet this isnt working...im still not sure how to put that into a class...it didnt work for me...
Private Sub SmsInterceptor_MessageRece
Try
' Cast to SmsMessage to access message body
' Not expecting to receive any non-SMS messages but use "as" to
' cast to be extra safe
Dim var_to As String
Dim var_message As String
Dim var_from As String
Dim newMessage As SmsMessage = TryCast(e.Message, SmsMessage)
' --------------------------
'
For x = 1 To 750000
If Not newMessage Is Nothing Then
' StatusBar1.Text = "From:" & newMessage.From.Address
Debug.WriteLine(String.For
' IF THE NEW MESSAGE IS A A NEW SMS SEND THE INFO
' TO THE APPLY_DB FUNCTION TO APPLY
' IT TO THE SQL DB FOR STORAGE
var_to = newMessage.To.ToString()
var_message = newMessage.Body.ToString()
var_from = newMessage.From.Address()
TextBox1.Text = var_to
TextBox2.Text = var_message
TextBox3.Text = var_from
apply_db(var_message, var_from)
Else
send_sms_main(sender, e)
TextBox5.Text = x
Label4.Visible = True
Label4.Text = "SYS ERR:"
TextBox4.Text = "PRG CMPT. REBOOT APP."
End If
Next
' --------------------------
Catch ex As Exception
Label2.Text = ex.Message
End Try
I think you're confusing SCOPE with FOCUS. The article states:
"...the MessageInterceptor class must remain in-scope otherwise it may stop monitoring."
If you declare the MessageInterceptor as local variable in a method, then when the method exits the variable goes out of scope, gets garbage collected, and then stops monitoring. You now have the variable at class level so this isn't an issue.
The MessageInterceptor can't have "focus" because it is not a GUI control...therefore it cannot lose focus either.
"...the MessageInterceptor class must remain in-scope otherwise it may stop monitoring."
If you declare the MessageInterceptor as local variable in a method, then when the method exits the variable goes out of scope, gets garbage collected, and then stops monitoring. You now have the variable at class level so this isn't an issue.
The MessageInterceptor can't have "focus" because it is not a GUI control...therefore it cannot lose focus either.
ASKER
Idle_mind...thanks for your comments...do you think this code is a good path?
Private Sub SmsInterceptor_MessageReceived(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)
Try
' Cast to SmsMessage to access message body
' Not expecting to receive any non-SMS messages but use "as" to
' cast to be extra safe
Dim var_to As String
Dim var_message As String
Dim var_from As String
Dim newMessage As SmsMessage = TryCast(e.Message, SmsMessage)
' ----------------------------------------------------
'
For x = 1 To 750000
If Not newMessage Is Nothing Then
' StatusBar1.Text = "From:" & newMessage.From.Address
Debug.WriteLine(String.Format("Sender:{0} - Body:{1}", newMessage.From.Address, newMessage.Body))
' IF THE NEW MESSAGE IS A A NEW SMS SEND THE INFO
' TO THE APPLY_DB FUNCTION TO APPLY
' IT TO THE SQL DB FOR STORAGE
var_to = newMessage.To.ToString()
var_message = newMessage.Body.ToString()
var_from = newMessage.From.Address()
TextBox1.Text = var_to
TextBox2.Text = var_message
TextBox3.Text = var_from
apply_db(var_message, var_from)
Else
send_sms_main(sender, e)
TextBox5.Text = x
Label4.Visible = True
Label4.Text = "SYS ERR:"
TextBox4.Text = "PRG CMPT. REBOOT APP."
End If
Next
' ----------------------------------------------------------------
Catch ex As Exception
Label2.Text = ex.Message
End Try
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am at aloss on how I can solve this problem..but I appreciate your help...
Just kick off each thread and let them die. They will share time (or do you want B to have priority over A . . . running until completion and then let A continue?)