Note that there are some explanatory texts on larger screens.

plurals
  1. POMove record insert to SQL Server CE database into another thread? C# Compact Framework 3.5
    primarykey
    data
    text
    <p>I had some trouble trying to place piece of code into another thread to increase performance.</p> <p>I have following code below (with thread additions with comments), where I parse large XML file (final goal 100,000 rows) and then write it to a SQL Server CE 3.5 database file (<code>.sdf</code>) using record and insert (<code>SqlCeResultSet/SqlCeUpdatableRecord</code>).</p> <p>Two lines of code in if statement inside the while loop, </p> <pre><code>xElem = (XElement)XNode.ReadFrom(xmlTextReader); </code></pre> <p>and </p> <pre><code>rs.Insert(record); </code></pre> <p>take about the same amount of time to execute. I was thinking to run <code>rs.Insert(record);</code> while I am parsing the next line of xml file. However, I still was unable to do it using either Thread or ThreadPool. </p> <p>I have to make sure that the record that I pass to thread is not changed until I finish executing <code>rs.Insert(record);</code> in existing thread. Thus, I tried to place thread.Join() before writing new record (<code>record.SetValue(i, values[i]);</code>), but I still get conflict when I try to run the program - program crashes with bunch of errors due to trying to write identical row several times (especially for index).</p> <p>Can anyone help me with some advise? How can I move <code>rs.Insert(record);</code> into another thread to increase performance?</p> <pre><code>XmlTextReader xmlTextReader = new XmlTextReader(modFunctions.InFName); XElement xElem = new XElement("item"); using (SqlCeConnection cn = new SqlCeConnection(connectionString)) { if (cn.State == ConnectionState.Closed) cn.Open(); using (SqlCeCommand cmd = new SqlCeCommand()) { cmd.Connection = cn; cmd.CommandText = "item"; cmd.CommandType = CommandType.TableDirect; using (SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable)) { SqlCeUpdatableRecord record = rs.CreateRecord(); // Thread code addition Thread t = new Thread(new ThreadStart(() =&gt; rs.Insert(record)); while (xmlTextReader.Read()) { if (xmlTextReader.NodeType == XmlNodeType.Element &amp;&amp; xmlTextReader.LocalName == "item" &amp;&amp; xmlTextReader.IsStartElement() == true) { xElem = (XElement)XNode.ReadFrom(xmlTextReader); values[0] = (string)xElem.Element("Index"); // 0 values[1] = (string)xElem.Element("Name"); // 1 ~~~ values[13] = (string)xElem.Element("Notes"); // 13 // Thread code addition -- Wait until previous thread finishes if (ThreadStartedS == 1) { t.Join() } // SetValues to record for (int i = 0; i &lt; values.Length; i++) { record.SetValue(i, values[i]); // 0 to 13 } // Thread code addition -- Start thread to execute rs.Insert(record) ThreadStartedS = 1; t.Start(); // Original code without threads // Insert Record //rs.Insert(record); } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload