B
Bob Day
VS 2003, VB.net...
Sometimes I get confused about when it is appropriate to use the word NEW.
In the code snipped below "Dim DT_With_Changes As New DataTable" works fine
with the NEW or without the NEW . This is a shared procedure, so I put in
the word NEW thinking that as multiple threads accessed it, each would be
working with its on instance of DT_With_Changes and I would not have to
worry about issuels like Monitor.Enter/Exit(DataTable_To_Update).
Intuitievely, with the word NEW, if each time a thread hits the code below,
even if at the same time, each thread would be working with its own instance
of DT_With_Changes . However, this didn't work, and the threads were
stepping on each other so I added the
Monitor.Enter/Exit(DataTable_To_Update).
1) In many cases, the use of NEW is obvious. Below, to me any way, it is
not. Can someone give me and explaination of when NEW is appropriate, and
what it doesn't solve the multiple thread access problem below?
2) also, DT_With_Changes.Dispose fails at runtime, so I changed it to
DT_With_Changes = nothing. But I don't understand why it fails. You get an
error message of something like "trying to dispose of a non-instantiated
object". Does seting it to nothing do the about same thing as dispose?
Thanks!
bob Day
' create DataTable to hold only the changes in table
Dim DT_With_Changes As New DataTable
Try
System.Threading.Monitor.Enter(DataTable_To_Update)
' load with rows that have changes in table
DT_With_Changes = DataTable_To_Update.GetChanges()
If Not DT_With_Changes Is Nothing Then
' update rows
Rows_Updated = SQL_Data_Adapter.Update(DT_With_Changes)
End If
' all rows for this table set back to unmodified.
DataTable_To_Update.AcceptChanges()
Finally
System.Threading.Monitor.Exit(DataTable_To_Update)
End Try
' dispose of temporary data table
DT_With_Changes = Nothing
Sometimes I get confused about when it is appropriate to use the word NEW.
In the code snipped below "Dim DT_With_Changes As New DataTable" works fine
with the NEW or without the NEW . This is a shared procedure, so I put in
the word NEW thinking that as multiple threads accessed it, each would be
working with its on instance of DT_With_Changes and I would not have to
worry about issuels like Monitor.Enter/Exit(DataTable_To_Update).
Intuitievely, with the word NEW, if each time a thread hits the code below,
even if at the same time, each thread would be working with its own instance
of DT_With_Changes . However, this didn't work, and the threads were
stepping on each other so I added the
Monitor.Enter/Exit(DataTable_To_Update).
1) In many cases, the use of NEW is obvious. Below, to me any way, it is
not. Can someone give me and explaination of when NEW is appropriate, and
what it doesn't solve the multiple thread access problem below?
2) also, DT_With_Changes.Dispose fails at runtime, so I changed it to
DT_With_Changes = nothing. But I don't understand why it fails. You get an
error message of something like "trying to dispose of a non-instantiated
object". Does seting it to nothing do the about same thing as dispose?
Thanks!
bob Day
' create DataTable to hold only the changes in table
Dim DT_With_Changes As New DataTable
Try
System.Threading.Monitor.Enter(DataTable_To_Update)
' load with rows that have changes in table
DT_With_Changes = DataTable_To_Update.GetChanges()
If Not DT_With_Changes Is Nothing Then
' update rows
Rows_Updated = SQL_Data_Adapter.Update(DT_With_Changes)
End If
' all rows for this table set back to unmodified.
DataTable_To_Update.AcceptChanges()
Finally
System.Threading.Monitor.Exit(DataTable_To_Update)
End Try
' dispose of temporary data table
DT_With_Changes = Nothing