B
Bob Day
VS 2003, vb.net , sql msde...
I have an application with multiple threads running. Its a telephony
application where each thread represents a telephone line. For code that
would be the same for each thread, I put in Shared methods as below. It is
only now that I am realizing the complexity of multiple threads accessing
shared methods. And, quite honestly, I am very confused.
I have tried System.Threading.Monitor.Enter, Synclock, etc., and still
cannot get things to work right. Honestly, it seems to be that for each
shared method, you need to do something to lock it in the 1st line and
unlock it when you exit. Otherwise, anything at anytime can be changed at
any moment by any thread in that method.
For example, if you have a variable loaded with a value at the top of the
method, that you then set a column to that variable while adding a row, you
cannot just use System.Threading.Monitor.Enter(DataRowToAdd) to protect the
row your are adding, because one of the variables might change by another
thread. You would need to use System.Threading.Monitor.Enter on everything
(variables, etc.) adding quite a bit of code. Even at that, I don't see
that System.Threading.Monitor.Enter stops another thread anyway.
I have spend a lot of time one this, and don't understand how to do it. Can
you explain to me how to write a shared method that adds rows to a data set,
and updates that dataset to the datasource, that can be used by multiple
threads at the same time. Any URLs would be helpf ul to. Maybe if I see a
working example, I will grasp what is going on.
Thanks!
Bob Day
Public NotInheritable Class DL
Public Shared Sub tblMETHODS_DETAILSC_ADD_Row(ByVal
tblMethods_Summary_Row_Number_0_Based As Integer, ByVal Summary_Text As
String, Optional ByVal Details_Text As String = "")
' code to add row to table methods_details and update that row to
datasource of same name
end sub
end class
I have an application with multiple threads running. Its a telephony
application where each thread represents a telephone line. For code that
would be the same for each thread, I put in Shared methods as below. It is
only now that I am realizing the complexity of multiple threads accessing
shared methods. And, quite honestly, I am very confused.
I have tried System.Threading.Monitor.Enter, Synclock, etc., and still
cannot get things to work right. Honestly, it seems to be that for each
shared method, you need to do something to lock it in the 1st line and
unlock it when you exit. Otherwise, anything at anytime can be changed at
any moment by any thread in that method.
For example, if you have a variable loaded with a value at the top of the
method, that you then set a column to that variable while adding a row, you
cannot just use System.Threading.Monitor.Enter(DataRowToAdd) to protect the
row your are adding, because one of the variables might change by another
thread. You would need to use System.Threading.Monitor.Enter on everything
(variables, etc.) adding quite a bit of code. Even at that, I don't see
that System.Threading.Monitor.Enter stops another thread anyway.
I have spend a lot of time one this, and don't understand how to do it. Can
you explain to me how to write a shared method that adds rows to a data set,
and updates that dataset to the datasource, that can be used by multiple
threads at the same time. Any URLs would be helpf ul to. Maybe if I see a
working example, I will grasp what is going on.
Thanks!
Bob Day
Public NotInheritable Class DL
Public Shared Sub tblMETHODS_DETAILSC_ADD_Row(ByVal
tblMethods_Summary_Row_Number_0_Based As Integer, ByVal Summary_Text As
String, Optional ByVal Details_Text As String = "")
' code to add row to table methods_details and update that row to
datasource of same name
end sub
end class