B
Bob Day
Using vs 2003, vb.net sql msde..
Consider the following code snippets. See **** for questions. All are
shared and accessed by multiple threads simultaneiously.
' Instantiate per for this shared class
Private Shared gcCDsd As New Class_Component_Designer
Private 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 = "", Optional ByVal
Exception_Thrown_Message As String = "")
****** Question 0) The same as question 2 below for the arguments above.
... code to create row to add
SyncLock (gcCDsd.DS_Methods.Methods_Details)
' Add new DataRow to DataSet
gcCDsd.DS_Methods.Methods_Details.AddMethods_DetailsRow(DR_To_Add)
' Call method to update row to datasourse
Rows_Updated = DLsd.tblMETHODS_DETAILS_UPDATE()
End SyncLock
end sub
Private Shared Function tblMETHODS_DETAILS_UPDATE() As Integer
' Purpose: Updates the DataSource From the DataSet for this table only
****** Question 1) Since gcCDsd.DS_Methods.Methods_Details is synclocked in
the calling method, does it need to be synclocked again here. I think not,
since it would seem to be already locked. If so, it would seem that the
same thread running into the same item with a 2nd synclock on it would
ignore the 2nd synclock. Is that thinking correct?
' Holds Rows affected by DataAdapter commands, such as FILL or UPDATED
Dim Rows_Updated As Integer = -1
' call method to update datasource
Rows_Updated = DL.cmdUPDATE_DataSource(gcCDsd.DA_tblMethods_Details,
gcCDsd.DS_Methods.Methods_Details)
' return rows updated
Return Rows_Updated
End Function
Friend Shared Function cmdUPDATE_DataSource(ByVal SQL_Data_Adapter As
SqlDataAdapter, ByVal DataTable_To_Update As DataTable) As Integer
' Purpose: Updates any changes made to a dataset datatable to the
datasource
****** Question 2) Do the parameters passed above need to be synclocked?
In other words, if 2 threads call this, do the arguments above instantiate a
new variable each time it is called (like dim SQL_Data_Adapter As
SqlDataAdapter would) or do they amount to shared variables that must be
protected by synclocks? It worked with out synclocks with 2 threads, but as
I have added threads I have had problems.
' Holds number of DS rows Updated to DataSource
Dim Rows_Updated As Integer = -1
****** Question 3) You would never synclock the next DIM line, since each
pass by a different thread (even at the same time) would create its own
DT_With_Changes to work with.
' create DataTable to hold only the changes in table
Dim DT_With_Changes As New DataTable
' load with rows that have changes in table
DT_With_Changes = DataTable_To_Update.GetChanges()
Rows_Updated = SQL_Data_Adapter.Update(DT_With_Changes)
' all rows for this table set back to unmodified.
DataTable_To_Update.AcceptChanges()
' Return number of rows updated6
Return Rows_Updated
End Function
Thanks!
Bob Day
Consider the following code snippets. See **** for questions. All are
shared and accessed by multiple threads simultaneiously.
' Instantiate per for this shared class
Private Shared gcCDsd As New Class_Component_Designer
Private 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 = "", Optional ByVal
Exception_Thrown_Message As String = "")
****** Question 0) The same as question 2 below for the arguments above.
... code to create row to add
SyncLock (gcCDsd.DS_Methods.Methods_Details)
' Add new DataRow to DataSet
gcCDsd.DS_Methods.Methods_Details.AddMethods_DetailsRow(DR_To_Add)
' Call method to update row to datasourse
Rows_Updated = DLsd.tblMETHODS_DETAILS_UPDATE()
End SyncLock
end sub
Private Shared Function tblMETHODS_DETAILS_UPDATE() As Integer
' Purpose: Updates the DataSource From the DataSet for this table only
****** Question 1) Since gcCDsd.DS_Methods.Methods_Details is synclocked in
the calling method, does it need to be synclocked again here. I think not,
since it would seem to be already locked. If so, it would seem that the
same thread running into the same item with a 2nd synclock on it would
ignore the 2nd synclock. Is that thinking correct?
' Holds Rows affected by DataAdapter commands, such as FILL or UPDATED
Dim Rows_Updated As Integer = -1
' call method to update datasource
Rows_Updated = DL.cmdUPDATE_DataSource(gcCDsd.DA_tblMethods_Details,
gcCDsd.DS_Methods.Methods_Details)
' return rows updated
Return Rows_Updated
End Function
Friend Shared Function cmdUPDATE_DataSource(ByVal SQL_Data_Adapter As
SqlDataAdapter, ByVal DataTable_To_Update As DataTable) As Integer
' Purpose: Updates any changes made to a dataset datatable to the
datasource
****** Question 2) Do the parameters passed above need to be synclocked?
In other words, if 2 threads call this, do the arguments above instantiate a
new variable each time it is called (like dim SQL_Data_Adapter As
SqlDataAdapter would) or do they amount to shared variables that must be
protected by synclocks? It worked with out synclocks with 2 threads, but as
I have added threads I have had problems.
' Holds number of DS rows Updated to DataSource
Dim Rows_Updated As Integer = -1
****** Question 3) You would never synclock the next DIM line, since each
pass by a different thread (even at the same time) would create its own
DT_With_Changes to work with.
' create DataTable to hold only the changes in table
Dim DT_With_Changes As New DataTable
' load with rows that have changes in table
DT_With_Changes = DataTable_To_Update.GetChanges()
Rows_Updated = SQL_Data_Adapter.Update(DT_With_Changes)
' all rows for this table set back to unmodified.
DataTable_To_Update.AcceptChanges()
' Return number of rows updated6
Return Rows_Updated
End Function
Thanks!
Bob Day