I'm still trying to wrap muy head around preventing thread conflicts. I have
a class that has two functions in it. This is called from various part of my
application and will generate a XML file. I've gotten lots of advice on
this, and am looking for a check on where I'm at. Am I missing some
concepts. Prior to this, I was using synclock to lock a filestream, however,
this time I'm not writing to a file, so not sure if I need a synclock or
not.
My concen is if the first function is called, and starts running the second
function while the first is called by another thread...would that lead to
the second function sending the wrong data back?
Public Class menuWriterToDB
Shared Sub createXMLfileWi thXmlWriter(ByV al parentNode As Integer, ByVal
intLevel As Integer, ByVal siteID As Integer)
Try
Dim sbXML As New System.Text.Str ingBuilder
Dim swXML As New System.IO.Strin gWriter(sbXML)
Dim twXML As New System.xml.XmlT extWriter(swXML )
twXML.Formattin g = twXML.Formattin g.Indented
twXML.Indentati on = 3
twXML.WriteStar tDocument()
twXML.WriteComm ent("Created on " & Now())
twXML.WriteStar tElement("menuI tems")
' this calls the other function to begin the recursive DB call and
building of the XML
WriteXmlData(tw XML, parentNode, siteID)
twXML.WriteEndE lement() 'close menuItems node
twXML.WriteEndD ocument() 'close docment
'create a DB connection and save the XML (sbXML.ToString ) into it
twXML.Flush()
twXML.Close()
Catch ex As Exception
' catch errors
Finally
End Try
End Sub
Shared Sub WriteXmlData(By Val objXMLWriter As System.Xml.XmlT extWriter,
ByVal parentNode As Integer, ByVal siteID As Integer, Optional ByVal
intLevel As Int32 = 0)
' grab all the data from the DB...
While rowCount < DS.Tables(0).Ro ws.Count 'ie, if there IS data,
then...
objXMLWriter.Wr iteStartElement ("menuItem")
' write out all the nodes...
objXMLWriter.Wr iteElementStrin g...
'now call the subroutine we're in to see if this value has
'any children and increase the indent, and so on...
WriteXmlData(ob jXMLWriter,
Convert.ToInt32 (DS.Tables(0).R ows(rowCount).I tem(0)), siteID, intLevel + 1)
rowCount = rowCount + 1
End While
' clean up
objConnect.Clos e()
objOleDbAdapter .Dispose()
objCommand.Disp ose()
End Sub
End Class